Add namespace to XmlTextWriter using C#

Posted by xml on Stack Overflow See other posts from Stack Overflow or by xml
Published on 2010-06-10T08:08:58Z Indexed on 2010/06/10 8:12 UTC
Read the original article Hit count: 273

Filed under:

Hi, I have an serializeable class that his root is serizlized to XmlRootAttribute with namespace. I want to add additional namespace to this root elemt, how can i do it? adding XmlAttribute failed to compile.

The code:

[System.Xml.Serialization.XmlRootAttribute("Root", Namespace = "http://www.w3.org/2003/05/soap-envelope", IsNullable = false)]
public class MyClass
{
    [System.Xml.Serialization.XmlElement("...")]
    public ClassA A;

    [System.Xml.Serialization.XmlElement("..")]
    public ClassB b;
}

After the serialization i'm getting something like that:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://www.w3.org/2003/05/soap-envelope">
<ClassA/>
<ClassB/>
</Envelope>

I want to add to the rood additioanl namespace, e.g. i want the xml to be:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      **xmlns:tns="anotherXml"** 
      xmlns="http://www.w3.org/2003/05/soap-envelope">
<ClassA/>
<ClassB/>
</Envelope> 

Any idea?

© Stack Overflow or respective owner

Related posts about xml-serialization