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
{
  [DataContractAttribute()]
  public class TestClass
  {
    [DataMember()]
    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }

    [DataMember()]
    public int Number
    {
      get { return _number; }
      set { _number = value; }
    }

    [DataMember()]
    public bool Enabled
    {
      get { return _enabled; }
      set { _enabled = value; }
    }

    [DataMember()]
    public List<string> Other
    {
      get { return _other; }
      set { _other = value; }
    }

    private string _name;
    private int _number;
    private bool _enabled;
    private List<string> _other = new List<string>();
  }
}

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:

xml_screenshot

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.

Share on LinkedIn   Share on Twitter

New Folks Moving to Town all the Time, Chief

A while back, I was reading a web page about how to report bugs in a useful manner and I was thinking, this is a good post, but I already know this. I realize that after 20 years in the software business, I am still eager to learn new things. That’s the gift and the curse of a career in software development, there’s always something new and spiffy to learn, but you have to keep at it or get swept downstream.

Continual improvement is a key to success. As I told my kids when they were younger (they’re old and wise teenagers now), even Tiger Woods has a swing coach.

My parents met while working as reporters for the Atlanta Constitution newspaper after WWII. They were sometimes sent to cover a human interest type story which was not what you would call “breaking news.”  The subject would sometime observe, “You all put a story in the paper a few years ago about this.” My mom said she would invariably reply “New folks moving to town all the time, chief.”

This brings me to my second point: passing on good information. If I know something well, and can recognize the good information when I see it, then good for me. Framing it for others to be able to make use of that hardwon wisdom is a whole different thing.

I’m starting a new category on this blog to highlight, reiterate, or refine information which may be out there, but could stand a burnishing or a revisit. After all, new folks are joining the software industry all the time…

Martin O. Waldron
Program Manager, SW Development
ImageSource, Inc.

Share on LinkedIn   Share on Twitter

Follow

Get every new post delivered to your Inbox.