BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


.NET and XML
Dynamic Method Invocation in .NET using Reflection API
Using XML classes in .Net
Pass node sets to XSLT stylesheets using .NET
Output a directory as XML using .NET
.Net, COM Interoperability and Word Object Model
WS-Routing for .NET endpoints
Using XML and XSLT in VB.NET Jeopardy
Symmetric Encryption/Decryption with .NET
XInclude.NET 1.0
EXSLT.NET 1.0
How to read a XML file using XmlTextReader?
How to compare a XML element with XmlTextReader?
How to verify if an element is empty with XmlTextReader and IsEmptyElement?
How to check if the XML elements contains any attributes with XmlTextReader
How to avoid to read the XML declaration with XmlTextReader?
How to create a XML file with XmlTextWriter?
Is it possible to add attributes with XmlTextWriter?
How to Write a Comment to a XML File with XmlTextWriter and WriteComment?
How to add the XML Declaration with XmlTextWriter and WriteStartDocument?
How to use Indentation in XmlTextWriter?


 
 

<< XSLT 


By Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First Posted 03/13/2002
Times viewed 325

XML Serialization and .Net


This post contains attachments
v20020313171333.zip 

Summary This article explains how to serialize an object to XML document and deserialize an xml document to the object.


XML Serialization is a process of converting an object's state into another form or format like XML or binary format where we can transport it easily.
   In .Net we can serialize an object by using System.Xml.Serialize namespace. It contains classes that are used to serialize objects into XML format documents or streams.
In this article I will explain how to seraialize an object to XML document,
deserialize an xml document to the object.

The main class in this namespace is XmlSerializer class. To use this class, use XmlSerializer constructor to create an instance of the class using the type of the object to serialize.
The following is the code which tells you how to create the instace of the class Person:
In C#:
 XmlSerializer oXS = new XmlSerializer(typeof(Person));

In VB:
 Dim oXS as XmlSerializer = new XmlSerializer(GetType(Person))
 
You can refer XmlSerial.cs or XmlSerial.vb, to know how the Class Person is created.

Once the XmlSerializer is created, create an instance of the object to serialize as follows:
In C#:
 Person oPandu = new Person();
In VB: 
 Dim oPandu As New Person()
 
Create an object to write the file to a document or stream as follows:
In C#:
 StreamWriter oStmW;
In VB:
 Dim oStmW As StreamWriter
 
Now you can call the Serailize method by writng the following code:
In C#:
 //Serialize object to XML and write it to XML file
 oStmW = new StreamWriter(Server.MapPath(pandu.xml));
 oXS.Serialize(oStmW, oPandu);
In VB:
 'Serialize object to XML and write it to XML file
 oStmW = new StreamWriter(Server.MapPath(pandu.xml))
 oXS.Serialize(oStmW, oPandu)

To deserialize an object from an Xml document, create an object to read the steam or document and then call Deserialize method as follows:
In C#:
 XmlSerializer oXS = new XmlSerializer(typeof(Person));
 Person oRao = new Person();
 StreamReader oStmR;

 oStmR = new StreamReader(Server.MapPath(rao.xml));
 oRao = (Person) oXS.Deserialize(oStmR);
In VB:
 Dim oRao As Person
 Dim oStmR As StreamReader
 Dim oXS as XMLSerializer = new XMLSerializer(GetType(Person))

 oStmR = New StreamReader(Server.MapPath(rao.xml))
 oRao = oXS.Deserialize(oStmR)

If you try to deserialize an XML document which does not exist, then an exception will be fired. You need to use try..catch statements to trap the exceptions.
Advantages of using Xml Serialization:
1. Gives you comprehensive and flexible control when you serialize an object to XML.
2. No contraints on the applications you develop, as long as the generated XML stream conforms to a given schema.
 
Source code is available to test the concept of XML Serialization. Run the XmlSerial.aspx file to test Serialize to File, Deserialize from File and Serialize to XML String

Additional information

Further additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< XSLT 





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML