Quantcast
Channel: Software Development for Enterprise Content Management » Software Industry
Viewing all articles
Browse latest Browse all 2

The DataContractSerializer – XML Files Made Easy

$
0
0

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



Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images