2

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.