The DataContractSerializer – XML Files Made Easy

With the release of .NET 3.5, Microsoft has included a fun new tool for data serializing.  The DataContractSerializer was introduced as a replacement for the current XmlSerializer class, and is the preferred XML data serializer of developers at ImageSource.

The DataContractSerializer class uses opt-in properties for it’s serialization rather than the XmlSerializer class’s opt-out methodology.  This means that the programmer must specify what class properties are to included in serialization.  It’s also up to the programmer to serialize fields, properties and non-public data.

The DataContractSerializer class is nice because it is easy to use and easy to read in the code.  It just requires that all serialized properties be tagged with the DataMember attribute and the class itself be tagged with the DataContractAttribute attibute. To use this class, a reference to the System.Runtime.Serialization library must be added to the project.

Below is an example of creating a serializable class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace TestNamespace
  public class TestClass
    public string Name
      get { return _name; }
      set { _name = value; }

    public int Number
      get { return _number; }
      set { _number = value; }

    public bool Enabled
      get { return _enabled; }
      set { _enabled = value; }

    public List Other
      get { return _other; }
      set { _other = value; }
    }    private string _name;
    private int _number;
    private bool _enabled;
    private List _other = new List();

Saving the class and loading it from a file is incredibly simple.  It just requires an instantiation of the DataContractSerializer class and a call to either WriteObject or ReadObject, depending on whether it is a save or load operation.

Below is an example of saving an object to an XML file and then loading it back from the disk:

using (FileStream writer = new FileStream("c:/temp/file.xml",
  FileMode.Create, FileAccess.Write))
  DataContractSerializer ser = new DataContractSerializer(typeof(TestClass));
  ser.WriteObject(writer, testObj);
}TestClass loadObj;
using (FileStream reader = new FileStream("c:/temp/file.xml",
  FileMode.Open, FileAccess.Read))
  DataContractSerializer ser = new DataContractSerializer(typeof(TestClass));
  loadObj = (TestClass)ser.ReadObject(reader);

Finally, here is an example of what that XML file looks like when opened in Internet Explorer:


So I hope this brief tutorial has been helpful.  The DataContractSerializer is nice for quickly creating objects for file settings, or when serialized XML data needs to be sent over a network.  It supports a wide variety of data types, as detailed on the MSDN site.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s