2010-06-10 63 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);