2010-06-10 62 views
2

我有一个可序列化的类,它的根用semlize命名空间XmlRootAttribute。 我想添加额外的命名空间到这个root elemt,我该怎么做呢?添加XmlAttribute无法编译。使用C将名称空间添加到XmlTextWriter#

代码:

[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; 
} 

系列化我得到类似的东西后:

<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> 

我想要添加到圣坛additioanl命名空间,例如我想要的XML是:

<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> 

任何想法?

回答

1

也许试试看:

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