473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

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

111 100+
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
Jul 5 '07 #1
0 6569

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: titanandrews | last post by:
Hi All, There is a library for Java called XStream which will serialize instances to XML. It's very easy to use and does not require changing existing code. Basically, you just call 1 API method...
2
by: David Elliott | last post by:
I can create this: ?xml version="1.0" standalone="yes" ?> <ConfigOpt> <record> <Field_1>Text # 1</Field_1> <Field_2>Text # 2</Field_2> </record> </ConfigOpt>
1
by: Glenn Wilson | last post by:
In the current project that I am planning i am thinking of serializing the objects to save as binary files. The question I have is with inheriting classes. If I have 1 base class and 2 classes...
2
by: tony lock | last post by:
I have a class inherited from Control, which I want to serialize, since Control is not Serializable, I have had to implement ISerializable. This works but I now want to inherit this base class into...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
5
by: Chris Szabo | last post by:
Good afternoon everyone. I'm running into a problem deserializing a stream using the XmlSerializer. A stored procedure returns the following from SQL Server: <Student StudentId="1" Status="1"...
9
by: zacks | last post by:
I have written a serialized class that has several properties that are typed as a list of type class. When I deserialize an XML file, the list is populated just fine. But I am having trouble...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.