Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 5th, 2007, 11:30 AM
Member
 
Join Date: Aug 2006
Location: USA
Age: 28
Posts: 90
Default How to create classes that can be Serialized by using XML Serialization:

Before going to that i want to say few thing on serialization :

Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream.

XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information. For example, if you have a Book object that exists in the Library namespace, there is no guarantee that it will be deserialized into an object of the same type.

Note XML serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). To serialize all an object's fields and properties, both public and private, use the BinaryFormatter instead of XML serialization. Private or protected members will be skipped during 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:

1) Specify the class as public.
2) Specify all members that must be serialized as public.
3) 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 User
{

public Int UserId;

public string Branch;

public string UserName;

public User()
{
}
}

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


<?xml version="1.0" ?>
<User>
<UserId>100</UserId>
<Branch>Development</Branch>
<UserName>"Ahmed"</UserName>
</User>

How to Control XML Serialization:

In this article I will be show a couple of theses attributes.

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

Change the User element name to DeptUser Make UserId an attribute of DeptUser, rather than a separate element.
Do not include the Branch in the serialized document.

[XmlRoot("DeptUser")]
public class User
{
[XmlAttribute]
public Int UserId;
[XmlIgnore]
public string Branch;
public string UserName;

public User()
{
}
}

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

<?xml version="1.0" ?>
<DeptUser UserId="100">
<UserName>"Ahmed"</UserName>
</DeptUser>

Summary:
1) To create a class that can be serialized, specify the class and all members as public, and create a parameterless constructor.
2) We 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.

Thanks & Regs
Bharath Reddy VasiReddy
Reply



Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.