April, 2010

  • Eric White's Blog

    Controlling Namespace Serialization of LINQ to XML

    • 0 Comments

    You can explicitly control how LINQ to XML serializes the namespaces for an XML tree.  You can control whether a namespace is serialized as the default namespace for a tree, or whether the namespace will use a prefix.  Namespace declarations look like attribute declarations, so to simplify the LINQ to XML tree, LINQ to XML contains the notion of ‘namespace attributes’.  These are XAttribute objects that are ‘attributes’ of an XElement object, but are there solely to control namespace serialization.  You can determine if an XAttribute object is a real attribute or a namespace attribute by looking at the IsNamespaceDeclaration property of the XAttribute object.  If you have an existing XML tree and you want to control its serialization, you can first remove all ‘namespace attributes’, and then add the namespace attributes that will cause the XML tree to be serialized as you wish.  There are two varieties of namespace attributes:

    • This blog is inactive.
      New blog: EricWhite.com/blog

      Blog TOC
      A namespace attribute that causes the namespace to be serialized as the default namespace.  This attribute has the special name of “xmlns”, and has the value of the namespace name.
    • A namespace attribute that causes the namespace to be serialized with a specific namespace prefix.  This attribute has the name of XNamespace.Xmlns + "x", where x is the desired prefix.  As with the attribute that causes serialization as the default namespace, its value is the namespace name.

    Serializing as the Default Namespace

    Consider the following small example:

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'>
            <a:Child>1</a:Child>
          </a:Root>");

    As written, this XML tree will serialize with a namespace prefix of ‘a’, but we can explicitly control this.  The following example first removes all ‘namespace attributes’, and then adds a namespace attribute that specifies that the namespace be serialized as the default namespace.  If you add a namespace attribute with the special name of “xmlns”, then than namespace will be serialized as the default namespace.

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'>
            <a:Child>1</a:Child>
          </a:Root>");
           
    // Remove all namespace attributes.
    root.DescendantsAndSelf().Attributes().Where(n => n.IsNamespaceDeclaration).Remove();

    // Specify that the namespace will be serialized as the default namespace.
    root.Add(new XAttribute("xmlns", "http://www.adventureworks.com"));

    Console.WriteLine(root);

    This outputs:

    <Rootxmlns="http://www.adventureworks.com">
      <Child>1</Child>
    </Root>

    Serializing with a Specific Namespace Prefix

    You can also change the namespace prefix for serialization.  If you add a namespace attribute with a name of XNamespace.Xmlns + “b”, then the namespace will serialize with an attribute of ‘b’:

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'>
            <a:Child>1</a:Child>
          </a:Root>");
           
    // Remove all namespace attributes.
    root.DescendantsAndSelf().Attributes().Where(n => n.IsNamespaceDeclaration).Remove();

    // Specify that the namespace will be serialized with a namespace prefix of 'b'.
    root.Add(new XAttribute(XNamespace.Xmlns + "b", "http://www.adventureworks.com"));

    Console.WriteLine(root);

    This causes the XML to be serialized like this:

    <b:Rootxmlns:b="http://www.adventureworks.com">
      <b:Child>1</b:Child>
    </b:Root>

    Controlling Serialization of Multiple Prefixes

    If you have elements or attributes in multiple namespaces in an XML tree, you can follow these same two techniques to control how each namespace is serialized.  The following XML tree serializes with the ‘http://www.adventureworks.com’ namespace with a prefix of ‘a’, and the ‘http://www.contoso.com’ namespace with a prefix of ‘c’.

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'
                  xmlns:c='http://www.contoso.com'>
            <c:Child>1</c:Child>
          </a:Root>");
    Console.WriteLine(root);
     

    If you want to serialize this XML tree with namespace prefixes of ‘x’ and ‘y’, you can do so like this:

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'
                  xmlns:c='http://www.contoso.com'>
            <c:Child>1</c:Child>
          </a:Root>");

    // Remove all namespace attributes.
    root.DescendantsAndSelf().Attributes().Where(n => n.IsNamespaceDeclaration).Remove();

    // Specify prefixes for both namespaces
    root.Add(
        new XAttribute(XNamespace.Xmlns + "x", "http://www.adventureworks.com"),
        new XAttribute(XNamespace.Xmlns + "y", "http://www.contoso.com"));

    Console.WriteLine(root);

    This produces the following output:

    <x:Rootxmlns:x="http://www.adventureworks.com"
            xmlns:y="http://www.contoso.com">
      <y:Child>1</y:Child>
    </x:Root>

    You could also decide to serialize one or the other namespace as the default namespace.

    XElement root = XElement.Parse(
        @"<a:Root xmlns:a='http://www.adventureworks.com'
                  xmlns:c='http://www.contoso.com'>
            <c:Child>1</c:Child>
          </a:Root>");

    // Remove all namespace attributes.
    root.DescendantsAndSelf().Attributes().Where(n => n.IsNamespaceDeclaration).Remove();

    // Specify prefixes for both namespaces
    root.Add(
        new XAttribute("xmlns", "http://www.adventureworks.com"),
        new XAttribute(XNamespace.Xmlns + "y", "http://www.contoso.com"));

    Console.WriteLine(root);

    This outputs:

    <Rootxmlns="http://www.adventureworks.com"
          xmlns:y="http://www.contoso.com">
      <y:Child>1</y:Child>
    </Root>

Page 4 of 7 (7 items) «23456»
Page 1 of 1 (7 items)