Microsoft KB Archive/314150

From BetaArchive Wiki

Article ID: 314150

Article Last Modified on 5/18/2007



APPLIES TO

  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1



This article was previously published under Q314150

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.

back to the top

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:

back to the top

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:

back to the top

Key Concepts and Quickstarts

The following links take you to documentation about key XML serialization and deserialization concepts and Quickstart tutorials.

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

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

back to the top

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


back to the top

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.

back to the top

Troubleshooting

If you experience problems and want assistance, the best place to start is at the Microsoft Product Support Services (PSS) Web site:

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.

back to the top

Keywords: kbarttyperoadmap kbinfo KB314150