XML Serialization with C# and .NET

Posted on Updated on


In the previous post, we discussed the basics of serialization in .NET. In this short article we are going to dive into some specific examples of use of the XmlSerializer to serialize objects to XML and deserialize from XML back into objects 🙂

XML Serialization with the System.Xml.Serialization.XmlSerializer class

To get started, let’s create a simple class, add some attributes to help us control how properties are serialized, then we’ll serialize and deserialize the object.

To get started, let’s create a C# Console Application that we will call SerializationSample.

Create the Serialization Sample Console Application
Create the Serialization Sample Console Application

Next, we will add a class to the project. We will call this class Sample.

Let’s now add two properties – Name and Value as shown below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerializationSample
{
    public class Sample
    {
        public string Name
        {
            get;
            set;
        }

        public Int32 Value
        {
            get;
            set;
        }
    }
}

As you can see, this class is very simple and is a great starting point for examining Xml serialization.

In our Program.cs class’s Main() method, we are going to write some simple code that uses the XmlSerializer to serialize an instance of our Sample object into an XML stream.

static void Main(string[] args)
{
    //let's first instantiate our Sample object
    //and set its two properties
    Sample sample = new Sample();
    sample.Name = "John Nelson";
    sample.Value = 44;

    //next, create an instance of an XmlSerializer
    //we will use the Type constructor for this example
    XmlSerializer serializer = new XmlSerializer(typeof(Sample));

    //serialize the object to the Console
    serializer.Serialize(Console.Out, sample);

    Console.ReadLine();
}

When we press F5 and run the console application, our console window displays the resulting XML. See the following illustration.

XMLSerializer.Serialize Output
XMLSerializer.Serialize Output

So when we look at the resulting XML, we see the standard xml declaration followed by the opening tag for our Sample object, an XML namespace (xmlns) attribute, and the values of the two properties in our object. Pretty simple, right?

XML Namespaces

Though my intent in this article is to discuss XML serialization and not XML and its accompanying concepts, we do need to briefly talk about the concept of XML namespaces. XML namespaces provide a couple of things in our generated XML. First, they remove any ambiguity between two or more elements that happen to have the same name. Second, they are useful for grouping elements that relate and adhere to a common “idea” together.

A namespace is identified and conveyed via a uri. So what does that mean? Well, I could define a namespace that I could use for all of my XML “objects” based on http://johnlnelson.com. I could for the sake of this example specify the namespace to be https://johnlnelson.com/namespaces/sample. It is a safe assumption that anyone who produces XML for the purpose of transmitting it via HTTP would do this via a domain, such as johnlnelson.com.

Armed with this little bit of knowledge, let’s modify our code to specify our own namespace and prefix.

static void Main(string[] args)
{
    //let's first instantiate our Sample object
    //and set its two properties
    Sample sample = new Sample();
    sample.Name = "John Nelson";
    sample.Value = 44;

    //next, create an instance of an XmlSerializer
    //we will use the Type constructor for this example
    XmlSerializer serializer = new XmlSerializer(typeof(Sample));

    //specify our namespace
    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
    namespaces.Add("jln", "https://johnlnelson.com/namespaces/sample");

    //serialize the object to the Console
    //with our specified namespace
    serializer.Serialize(Console.Out, sample, namespaces);

    Console.ReadLine();
}

Pay attention to lines 14-15 and the new overload of the Serialize() method in line 19.

We created an instance of an XmlSerializerNamespaces object and added one prefix/namespace combination. The prefix I chose is “jln” and my namespace is https://johnlnelson.com/namespaces/sample.

When we press F5 to launch the application and view our XML output, we see our own namespace in the XML.

XMLSerializer.Serialize Output with Namespace
XMLSerializer.Serialize Output with Namespace

Pretty simple? Sure it is. Now let’s take a look at the encoding attribute in our xml document tag. What if we don’t want to use the default IBM437 encoding? We can do that too, but the code is slightly more involved (but not too bad).

Let’s modify our serialization code to look like this:

//let's first instantiate our Sample object
//and set its two properties
Sample sample = new Sample();
sample.Name = "John Nelson";
sample.Value = 44;

//next, create an instance of an XmlSerializer
//we will use the Type constructor for this example
XmlSerializer serializer = new XmlSerializer(typeof(Sample));

//specify our namespace
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("jln", "https://johnlnelson.com/namespaces/sample");

//create an XmlWriterSettings object to specify the
//encoding and the indentation
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding();
settings.Indent = true;

//create an XmlWriter that utilizes a StringWriter to
//build the output, then write that to the Console window
using (StringWriter stringWriter = new StringWriter())
{
    using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, settings))
    {
        serializer.Serialize(xmlWriter, sample, namespaces);
        Console.Write(stringWriter);
    }
}

Console.ReadLine();

You will see quite a few changes from lines 17 to 30. We will discuss the changes in a bit, but first let’s press F5 and run our code! The console window will now look like this:

XMLSerializer.Serialize() Output
XMLSerializer.Serialize() Output

Okay, so what’s going on here? First, in order to take the level of control over our encoding, we had to initiate the help of a StringWriter and an XmlWriter. We created an XmlWriterSettings object to allow us to specify the encoding and the indentation for our XML output. Then we wrapped the XmlWriter in a StringWriter which we then used to send our output to the Console window.

We won’t go into too much detail about StringWriters and XmlWriters in this article, but let’s just take from this the fact that it is possible to exert varying levels of control over our XML serialization.

Controlling How We Serialize

Before we jump into deserialization, let’s hop back to our Sample class and take a look at some ways we can control how instances of this type are XML serialized. This sample class has two properties: Name and Value. As it stands now, both properties get serialized when we call the XmlSerializer.Serialize() method. Let’s look at some ways we can control this.

System.Xml.Serialization.XmlIgnoreAttribute

Using the XmlIgnore attribute we can instruct the XmlSerializer to ignore public fields or properties when XML serialization takes place. For this example, let’s add an XmlIgnore attribute to the Value property as shown below:

[XmlIgnore]
public Int32 Value
{
    get;
    set;
}

By adding this simple attribute, the XmlSerializer will not serialize this property. Let’s press F5 and give it a try.

When we place a breakpoint and view the XML output in the XML view window, we notice that the Value property did NOT get serialized.

XmlSerializer.Serialize() method with Value property ignored
XmlSerializer.Serialize() method with Value property ignored

Pretty simple, right?

System.Xml.Serialization.XmlElementAttribute

What if we want to serialize a property, but we want the XmlElement to have a name other than the actual name in the object? This is equally simple by decorating that property with an XmlElement attribute. See the code below.

[XmlElement (ElementName="TheValue")]
public Int32 Value
{
    get;
    set;
}

This attribute instructs the XmlSerializer to serialize the property, but to name the XmlElement “TheValue”. Again, we place a breakpoint and view the output:

XmlSerializer.Serialize() method with XmlElement attribute on Value property
XmlSerializer.Serialize() method with XmlElement attribute on Value property

As we can see in the output, the Value field is serialized as TheValue. Pretty simple.

There are more customization options available through attributes, but we are not going to cover all of them here. This MSDN resource is an excellent source of information.

Deserializing XML with C#

Now that we have discussed XML serialization, let’s look quickly at how to deserialize our XML and re-inflate a Sample object.

We will modify the code in our Program class’s Main() method to look like this:

//let's first instantiate our Sample object
//and set its two properties
Sample sample = new Sample();
sample.Name = "John Nelson";
sample.Value = 44;

//next, create an instance of an XmlSerializer
//we will use the Type constructor for this example
XmlSerializer serializer = new XmlSerializer(typeof(Sample));

//specify our namespace
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("jln", "https://johnlnelson.com/namespaces/sample");

//serialize the object to a StringWriter
//with our specified namespace
StringWriter writer = new StringWriter();
serializer.Serialize(writer, sample, namespaces);

//for our subsequent deserialization, we will
//get the StringWriter's underlying StringBuilder
string xml = writer.GetStringBuilder().ToString();

//we will create a new StringReader using
//the xml string created above
TextReader reader = new StringReader(xml);

//we will deserialize the reader and cast
//the resulting object to a Sample type
Sample deserialized = (Sample)serializer.Deserialize(reader);

//finally, we will write our object to the Console
Console.WriteLine("We have deserialized our object");
Console.WriteLine(String.Format("Name: {0}", deserialized.Name));
Console.WriteLine(String.Format("Value: {0}", deserialized.Value.ToString()));
Console.WriteLine("That was cool!");

Console.ReadLine();

The commentary in the code is pretty straightforward. We serialized our object into a StringWriter then got that object’s underlying StringBuilder and got its string representation via the xml string object. Then we created a StringReader to utilize the XML string value as an input to our XmlSerializer.Deserialize() method. We immediately cast the return object to the Sample type because we knew that it would work. In most situations like this, should NOT attempt such a cast. Instead, you should stuff the result into an object, then check for null BEFORE attempting the cast. That is just a good programming practice.

When we press F5 to run the application, we see the following output:

XMLSerializer.Deserialize() Output
XMLSerializer.Deserialize() Output

In the next article, we will take a quick look at binary serialization.

7 thoughts on “XML Serialization with C# and .NET

    dineshramitc said:
    July 2, 2014 at 1:39 pm

    Reblogged this on Dinesh Ram Kali..

    Like

    sergiobotta said:
    March 10, 2019 at 7:12 pm

    I had to edit the last post becase it was not correct:

    Hi! I need class “Sample” to be “ns:Sample” .. I mean .. in XML file it should be

    Where can I specify this?
    Thanks!

    Like

    danielsalum said:
    July 30, 2019 at 11:11 am

    Hola John, Le hago una consulta para ver si puede ayudarme: Estoy serializando un objeto a XML. Algunos atributos nulos del xml quedan asì:

    XmlSerializerNamespaces _NS = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    XmlSerializer _Serializer = new XmlSerializer(_Msj.GetType());
    XmlWriterSettings _Settings = new XmlWriterSettings
    {Indent = true, OmitXmlDeclaration = true};
    string _Path = Path.GetTempPath();
    string _Archivo = _Path + “ICR_Cliente.xml”;
    using (XmlWriter _XmlWriter = XmlWriter.Create(_Archivo, _Settings))
    {_Serializer.Serialize(_XmlWriter, _Msj, _NS);}
    dynamic _XML;
    using (StreamReader _XmlRead = File.OpenText(_Archivo))
    {_XML = _XmlRead.ReadToEnd();}

    Like

    danielsalum said:
    July 30, 2019 at 11:13 am

    Quisiera que queden asi:

    No encuentro la forma de como hacerlo; tal vez me pueda ayudar a resolverlo.
    saludos y gracias
    Daniel

    Like

    danielsalum said:
    July 30, 2019 at 11:52 am

    Quisiera que queden asi:

    Like

    danielsalum said:
    July 30, 2019 at 11:53 am

    .
    Quisiera que queden asi:
    .

    Like

    danielsalum said:
    July 30, 2019 at 11:54 am

    PrioridadFacturacion p2:nil=”true” xmlns:p2=”http://www.w3.org/2001/XMLSchema-instance”
    Quisiera que queden asi:
    PrioridadFacturacion nil=”true””

    Like

Leave a comment