INFO: Roadmap for XML Serialization in the .NET Framework

Article translations Article translations
Article ID: 314150
This article was previously published under Q314150
Expand all | Collapse all

On This Page

Summary

This article provides a roadmap to learn and to master serialization and deserialization of XML with the .NET Framework (System.Xml namespace).

Roadmap articles provide links to useful information, including online documentation, Microsoft Knowledge Base articles, and Microsoft white papers, to help you learn about a Microsoft product or technology. Microsoft Knowledge Base How To articles and walkthroughs provide step-by-step instructions to complete specific tasks. QuickStart sample files are ready-made programs that illustrate a technique.

Overview

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

The .NET Framework features two serializing technologies:
  • Binary serialization preserves type fidelity, which is useful for preserving the state of an object between different invocations of an application. For example, you can share an object between different applications by serializing it to the clipboard. For example, you can serialize an object to a stream, to a disk, to memory, or over the network. Remoting uses serialization to pass objects "by value" from one computer or application domain to another.
  • 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.
It is important to understand the difference between these two technologies. XML serialization does not convert methods, indexers, private fields, or read-only properties (except read-only collections). To serialize all of the public and private fields and properties of an object, use the BinaryFormatter instead of XML serialization.

The following links provide overview information about XML serialization and deserialization in Microsoft Visual Studio .NET:
Introducing XML Serialization
http://msdn2.microsoft.com/en-us/library/182eeyhh(vs.71).aspx

XML and SOAP Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconserialization.asp

XML Serialization Architecture

The System.Xml.Serialization namespace contains the classes that are used to serialize objects into XML format documents or streams.

The central class in the namespace is the XmlSerializer class. To use this class, use the constructor of the XmlSerializer to create an instance of the class by using the Type of the object to serialize. After an XmlSerializer is created, create an instance of the object to serialize. You must also create an object to write the file to a document or stream, such as a Stream, a TextWriter, or an XmlWriter. You can then call the Serialize method to convert the object to an XML document.

To deserialize an object from an XML document, create a suitable object to read the document or stream (again a Stream, a TextWriter, or an XmlWriter). Invoke the Deserialize method while casting the resulting object to the Type of the original object that was serialized.

To control the serialization more, the System.Xml.Serialization namespace provides several Attribute classes that can be applied to members of a class. For example, if a class contains a member that will be serialized as an XML element, you can apply the XmlElementAttribute attribute to the member. When applying the attribute, you can specify details such as the actual XML element name by using the ElementName property.

The following link provides architectural (internal) information about XML serialization and deserialization:
XML Serialization Class
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(vs.71).aspx

System.Xml.Serialization Hierarchy
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationhierarchy.asp

Key Concepts and Quickstarts

The following links take you to documentation about key XML serialization and deserialization concepts and Quickstart tutorials.
XML Serialization with XML Web Services
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconxmlserializationwithwebservices.asp

Overriding XML Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconoverridingserializationofclasseswithxmlattributeoverridesclass.asp

.NET Samples - How To: XML Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpqstart/html/cpsmpnetsamples-howtoxmlserialization.asp
The "Examples of XML Serialization" link gives several code samples that demonstrate XML serialization, including the following:
  • Serialization of a DataSet
  • Serialization of an XmlElement and XmlNode
  • A Class Containing a Field Returning a Complex Object Serialized
  • An Array of Objects Serialized
  • Serializing a Class that Implements the ICollection Interface
  • A Purchase Order Example
Examples of XML Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconanexampleofxmlserializationwithxmlserializer.asp
The "Controlling XML Serialization Using Attributes" link discusses the following topics with code samples:
  • Controlling Array Serialization
  • Serializing Derived Classes
  • Serializing an Array as a Sequence of Elements
  • Serializing an ArrayList
  • Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute
  • Preventing Serialization With the XmlIgnoreAttribute
Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrollingserializationbyxmlserializerwithattributes.asp

Knowledge Base Articles

Microsoft Knowledge Base How To articles provide step-by-step instructions to complete a specific task.
315703 HOW TO: Serialize an Object to XML by Using Visual Basic .NET
316730 HOW TO: Serialize and Deserialize XML in Visual Basic .NET
You can also run XSL transformations (XSLT), query and modify XML data by using DOM, and serialize or deserialize XML data by using the System.Xml classes in the .NET Framework. For more information, see the following Knowledge Base article:
313651 Roadmap for XML in the .NET Framework

Walkthroughs

Walkthroughs provide mini-tutorials that walk you through some typical application development scenarios that use XML serialization and deserialization. The following link will take you to walkthrough documents.
Using Schema and Serialization to Leverage Business Logic
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml04162001.asp

Troubleshooting

If you experience problems and want assistance, the best place to start is at the Microsoft Product Support Services (PSS) Web site:
http://support.microsoft.com
You have several options to find answers to your questions. You can search the Microsoft Knowledge Base, post your questions or share your experiences on Microsoft newsgroups, obtain the latest service packs and news about a specific product, or contact Microsoft Product Support.

Properties

Article ID: 314150 - Last Review: December 12, 2012 - Revision: 4.0
Keywords: 
kbarttyperoadmap kbinfo KB314150
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com