January 25, 2021
Hot Topics:

Serialization/Deserialization in .NET

  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Welcome to the next installment of the .NET Nuts & Bolts. In this column, we'll explore serialization and deserialization with the Microsoft .NET Framework. We'll look at the reasons for using serialization and techniques for utilizing it within your applications. It will involve using classes located in the System.Runtime.Serialization namespace.

Definition of Serialization

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or persisted to a storage location. The storage location could be as simple as using a file or a database. The serialized format contains the object's state information. Deserialization is the process of using the serialized state information to reconstruct the object from the serialized state to its original state. In essence, the process of serialization allows an object to be serialized, shipped across the network for remoting or persisted in a storage location such as the ASP.NET cache, and then be reconstructed for use at a later point in time.

Serialization Formats

There are three formats provided by the Microsoft .NET framework to which objects can be serialized. The formats are binary, SOAP, and XML. The format is controlled based upon what object is used to perform the serialization. The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

There are subtle differences among the serialized formats. The binary-based format is the most compact and light of the three formats. The XML formatter only serializes public fields and properties, while binary and SOAP do not adhere to that limitation.

Controlling XML Serialization with Attributes

Attributes can be used to control the serialization of an object performed by the XmlSerializer. Some of the attributes available are as follows:

  • XmlAttributeAttribute—The member will be serialized as an XML attribute
  • XmlElementAttribute—The field or property will be serialized as an XML element
  • XmlIgnoreAttribute—The field or property will be ignored when serializing
  • XmlRootAttribute—Represents the XML document's root element name, only applies to a class

XML Serialization Sample Code

The following code defines a class that demonstrates the use of some of the XML serialization controlling attributes.

using System;using System.Xml.Serialization;namespace CodeGuru.Serialization{  [XmlRoot("TestDataXml")]  public class TestData  {    private int _Identity = 0;    private string _Name = "";    [XmlElement("DataName")]    public string Name    {      get { return this._Name; }      set { this._Name = value; }    }    private string _IgnoreMe = "";    [XmlIgnore]    public string IgnoreMe    {      get { return this._IgnoreMe; }      set { this._IgnoreMe = value; }    }    public TestData()    {    }  }}

Test XML Serialization Code

The following code will serialize the sample class defined above and store the information in a file on the local drive.

TestData obj = new TestData();obj.Name     = "testing";obj.IgnoreMe = "ignore";XmlSerializer serializer = new XmlSerializer(obj.GetType());Stream stream = new FileStream("c:\\MyFile.xml", FileMode.Create,       FileAccess.Write, FileShare.None);serializer.Serialize(stream, obj);stream.Close();

Resulting XML

Notice how the private fields and properties are not included in the XML. The XmlSerializer behaves this way by design.

<?xml version="1.0"?><TestDataXml xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  <DataName>testing</DataName></TestDataXml>

Page 1 of 2

This article was originally published on November 18, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date