Adding namespace prefix to Root node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scottpet@gmail.com

    Adding namespace prefix to Root node

    Hi,

    I want to add a namespace prefix to the root node of an object I am
    serializing to XML.

    I have been reading though this article:
    Specifies that the target property, parameter, return value, or class member contains prefixes associated with namespaces that are used within an XML document.


    I get namespace prefixes in the document, but not on the root node.
    Specifically, the serializer does not work as described in the last
    paragraph:

    (from http://msdn2.microsoft.com/en-gb/lib...attribute.aspx)

    " For example, in the following XML document, only the prefix pair
    "cal" is captured, but not the "x" prefix. To get that data, add a
    member with the XmlNamespaceDec larationsAttrib ute to the class that
    represents the root element."

    If you look through the sample, they already use the
    XmlNamespaceDec larationsAttrib ute and there is no way to associate the
    instance of the XmlNamespaceDec larationsAttrib ute with the root node.

    Is this a bug or a feature?

    Thanks -Scott

  • scottpet@gmail.com

    #2
    Re: Adding namespace prefix to Root node

    Ok..figured it out...you have to load the resulting document into an
    XmlDocument type, then use the DocumentElement .Prefix property to set
    the prefix of the root node...works perfectly. It's unfortunate to
    have to do that since that is a common scenario.

    Comment

    • Martin Honnen

      #3
      Re: Adding namespace prefix to Root node

      scottpet@gmail. com wrote:
      I want to add a namespace prefix to the root node of an object I am
      serializing to XML.
      Here is an example doing that:

      XmlSerializerNa mespaces namespaces = new XmlSerializerNa mespaces();
      namespaces.Add( "prefix", "http://example.com/2007/ns1");
      XmlSerializer serializer = new XmlSerializer(t ypeof(Example)) ;

      Example example = new Example();
      example.Name = "Kibo";
      example.Power = 42;
      serializer.Seri alize(Console.O ut, example, namespaces);
      Console.WriteLi ne();

      [XmlRoot(Namespa ce = "http://example.com/2007/ns1")]
      public class Example {
      public Example () {}
      public string Name;
      public int Power;
      }


      The output is:

      <prefix:Examp le xmlns:prefix="h ttp://example.com/2007/ns1">
      <prefix:Name>Ki bo</prefix:Name>
      <prefix:Power>4 2</prefix:Power>
      </prefix:Example>


      Is that what you want to achieve? Or do you want the namespace only on
      the root element? Then you need to use e.g.

      [XmlRoot(Namespa ce = "http://example.com/2007/ns1")]
      public class Example {
      public Example () {}
      [XmlElement(Name space = "")]
      public string Name;
      [XmlElement(Name space = "")]
      public int Power;
      }


      --

      Martin Honnen --- MVP XML

      Comment

      Working...