XmlSerializer and Namespaces

Need to share some data with another system/program/thing? XML files are a great, easy way to do it! Typically, my process for creating or consuming XML files will be to generate a class from an XML schema definition (XSD) and then serialize to and deserialize from XML as needed.

Usually, you don’t need to do anything above and beyond this, but from time to time you might need to do some additional manipulation with the XML namespaces and associated prefixes. The XmlSerializer class gives you a lot of flexibility to do this, and it’s really easy! Here are some different things you can do along with the output produced.

Here’s my no-frills Person class that I’ll be serializing to XML in the examples that follow:

[XmlType(Namespace = "http://adamprescott/sample")]
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? DateOfBirth { get; set; }
}

By default, the serializer will include the XML namespace on each element.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person));
serializer.Serialize(Console.Out, p);
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FirstName xmlns="http://adamprescott/sample">James Q.</FirstName>
  <LastName xmlns="http://adamprescott/sample">Sample</LastName>
  <DateOfBirth xmlns="http://adamprescott/sample">1970-01-01T00:00:00</DateOfBirth>
</Person>

Whoa-day! That’s pretty busy. You can clean it up a lot just by declaring the default namespace when you instantiate your XmlSerializer.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person), "http://adamprescott/sample");
serializer.Serialize(Console.Out, p);
<?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://adamprescott/sample">
  <FirstName>James Q.</FirstName>
  <LastName>Sample</LastName>
  <DateOfBirth>1970-01-01T00:00:00</DateOfBirth>
</Person>

That’s a little nicer, but what if I don’t want those XMLSchema namespaces? That’s easy, too!

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person), "http://adamprescott/sample");
var namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, "http://adamprescott/sample");
serializer.Serialize(Console.Out, p, namespaces);
<?xml version="1.0"?>
<Person xmlns="http://adamprescott/sample">
  <FirstName>James Q.</FirstName>
  <LastName>Sample</LastName>
  <DateOfBirth>1970-01-01T00:00:00</DateOfBirth>
</Person>

Note that I made the prefixes all string.Empty. If I were to specify a prefix, it would be included in the serialized XML.

var p = new Person
    {
        LastName = "Sample",
        FirstName = "James Q.",
        DateOfBirth = new DateTime(1970, 1, 1),
    };
var serializer = new XmlSerializer(typeof(Person));
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("ap", "http://adamprescott/sample");
serializer.Serialize(Console.Out, p, namespaces);
<?xml version="1.0"?>
<Person xmlns:ap="http://adamprescott/sample">
  <ap:FirstName>James Q.</ap:FirstName>
  <ap:LastName>Sample</ap:LastName>
  <ap:DateOfBirth>1970-01-01T00:00:00</ap:DateOfBirth>
</Person>
Advertisements

Leave a comment

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s