SIGN UP MEMBER LOGIN:    
ARTICLE

XML Serialization in .NET Framework 2.0 : Part II

Posted by Khaled Samir Articles | Visual C# March 23, 2007
This article shows how to create classes that can be Serialized by using XML Serialization.
Reader Level:

Introduction:

In the previous
article we have learnt how to use XML Serialization with .NET Framework Data Types. In this article we will learn how to make your class support XML Serialization.

How to create classes that can be Serialized by using XML Serialization:

To create a class that can be serialized by using XML serialization, you must perform the following tasks:

  • Specify the class as public.
  • Specify all members that must be serialized as public.
  • Create a parameterless constructor.

Unlike classes processed with standard serialization, classes do not have to have the Serializable attribute to be processed with XML serialization. If there are private or protected members, they will be skipped during serialization.

Example:

public class Student

{

    public Int32 studentId;

    public decimal age;

    public string studentName;

 

    public Student()

    {

 

    }
}

Serializing an instance of this class with sample values creates the following XML (which has been slightly simplified for readability):


<?
xml version="1.0" ?>

<Student>

  <studentId>100</studentId>

  <age>15.5</age>

  <studentName>"Ahmed"</studentName>
</Student>

How to Control XML Serialization:

You can control XML Serialization using some attributes in .NET Framework 2.0. You can find them in
http://msdn2.microsoft.com/en-us/library/2baksw0z.aspx

For the scope of the this article we will be exposed to a couple of theses attributes.

For example, consider the attributes required to make the following three changes to the serialized XML document:

  1. Change the Student element name to SchoolStudent.
  2. Make studentId an attribute of SchoolStudent, rather than a separate element.
  3. Do not include the age in the serialized document.

[XmlRoot("SchoolStudent")]

public class Student

{

    [XmlAttribute]

    public Int32 studentId;

    [XmlIgnore]

    public decimal age;

    public string studentName;

 

    public Student()

    {

 

    }
}

Serializing an instance of this class with sample values creates the following XML(which has been slightly simplified for readability):

<?xml version="1.0" ?>

<SchoolStudent studentId="100">

  <studentName>"Ahmed"</studentName>

</SchoolStudent>

Summary:

  • To create a class that can be serialized, specify the class and all members as public, and create a parameterless constructor.
  • You can control XML serialization by using attributes. Attributes can change the names of elements, serialize members as attributes rather than elements, and exclude members from serialization.

Login to add your contents and source code to this article
share this article :
post comment
 

i have created a program where i am able to draw on my form so i need to save my graph as an xml file on C drive

Posted by Jacob Bahula Jul 06, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor