views:

127

answers:

1

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"&gt;
<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"&gt;
<ClassA/>
<ClassB/>
</Envelope> 

Any idea?

+1  A: 

Maybe try this out:

XmlSerializerNamespaces XMLNamespaces = =new XmlSerializerNamespaces();
        XMLNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        XMLNamespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
        XMLNamespaces.Add("tns", "anotherXml");

XMLSerializer.Serialize(XMLWriter, inputObject, XMLNamespaces);
Barry