开发者

Add namespace to XmlTextWriter using C#

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-e开发者_如何学运维nvelope">
<ClassA/>
<ClassB/>
</Envelope> 

Any idea?


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜