473,837 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

111 New Member
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>Develop ment</Branch>
<UserName>"Ahme d"</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("DeptUs er")]
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>"Ahme d"</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 6616

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

Similar topics

7
2027
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 and bingo, you have XML. I was wondering if there is something similar for C++. I have already investigated the boost serialization library, but it requires adding a serialize() function to your classes. Does anyone know of an open source...
2
9106
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
1906
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 that inherit from it. How would I configure them for serialization. Do I just put the Serialized setting on the 2 classes or do I also have to put it on the base class. ie.
2
5081
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 a number of other classes, I was assuming that I could just mark them as and use automatic serialization but this does not work (The new class cannot deserialize, because it does not have the correct constructor ie one with arguments...
6
6057
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 problem is that in my derived classes, the XML that is automatically generated is lacking the properties of the base class. For example: public class MyBaseClass { public MyBaseClass ( ) { } private string myVariable;
5
2011
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" Gpa="3.50"> <Person Id="1" FirstName="FirstName0" LastName="LastName0" MiddleInitial="W"/> </Student> In my code, person is the base class and student extends it. When I
9
1525
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 manually loading the class for serialization when the class has not been initialized by a deserialize. If I add three instances of a class to the list, each with different values for their properties, the XML file created by serialization has three...
2
6594
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){} I then determine what object I should call the Persist method on using a switch statement (not very OO). switch (otype)
10
4789
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 value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml schema. This all sounds simple, but SQL Server REQUIRES the timezone to be specified for date values....
0
9683
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10633
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10276
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7814
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5852
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.