Microsoft KB Archive/815813

= How to serialize an object to XML by using Visual C# =

Article ID: 815813

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-





For a Microsoft Visual Basic .NET version of this article, see 315703.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Xml
 * System.Xml.Serialization

IN THIS TASK

 * SUMMARY
 * Requirements
 * XML Serialization
 * Complete Code Listing
 * Verification
 * Troubleshooting
 * REFERENCES



SUMMARY
This step-by-step article describes how to serialize an object to XML by using Visual C# .NET. This method is useful for persisting the state of an object. This method is also useful for cloning an object by de-serializing the XML back to a new object.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * General familiarity with XML
 * General familiarity with Visual C#

back to the top

XML Serialization
Serialization is the process of taking the state of an object and persisting it in some fashion. The Microsoft .NET Framework includes powerful objects that can serialize any object to XML. The System.Xml.Serialization namespace provides this capability.

Follow these steps to create a console application that creates an object, and then serializes its state to XML:  In Visual C# .NET or in Visual C# 2005, create a new Console Application project. On the Project menu, click Add Class to add a new class to the project. In the Add New Item dialog box, change the name of the class to clsPerson . Click Open. A new class is created.

Note In Visual Studio 2005, click Add.  Add the following code after the Public Class clsPerson statement public  string FirstName; public  string MI; public  string LastName;  Switch to the code window for Class1.cs in Visual Studio .NET or for Program.cs in Visual Studio 2005.  In the void Main method, declare and create an instance of the clsPerson class: clsPerson p = new clsPerson; </li>  Set the properties of the clsPerson object:

p.FirstName = &quot;Jeff&quot;; p.MI = &quot;A&quot;; p.LastName = &quot;Price&quot;; </li>  The Xml.Serialization namespace contains an XmlSerializer class that serializes an object to XML. When you create an instance of XmlSerializer, you pass the type of the class that you want to serialize into its constructor: System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType); </li>  The Serialize method is used to serialize an object to XML. Serialize is overloaded and can send output to a TextWriter, Stream, or XMLWriter object. In this example, you send the output to the console: x.Serialize(Console.Out,p); Console.WriteLine; Console.ReadLine; </li></ol>

back to the top

Complete Code Listing
using System;

public class clsPerson { public  string FirstName; public string MI; public string LastName; }

class class1 {   static void Main(string[] args) {     clsPerson p=new clsPerson; p.FirstName = &quot;Jeff&quot;; p.MI = &quot;A&quot;; p.LastName = &quot;Price&quot;; System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType); x.Serialize(Console.Out, p); Console.WriteLine; Console.ReadLine; } }   back to the top

Verification
To verify that your project works, press CTRL+F5 to run the project. A clsPerson object is created and populated with the values that you entered. This state is serialized to XML. The console window shows the following: <?xml version=&quot;1.0&quot; encoding=&quot;IBM437&quot;?> <clsPerson xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3 .org/2001/XMLSchema&quot;> <FirstName>Jeff</FirstName> <MI>A</MI>

<LastName>Price</LastName> </clsPerson> back to the top

Troubleshoot
The Xml.Serialization.XmlSerializer object performs only shallow serialization. If you also want to serialize the private variables of an object or child objects, you must use deep serialization.

back to the top

<div class="references_section">