2015-10-20 51 views
0

我試圖用不同的xmlrootname「ns1:BatchChanges」對序列化類JTDChanges進行序列化,但在將其寫入文件後,「ns1:BatchChanges」被編碼爲「ns1_x003A_BatchChanges」。如何避免在xmlroot中編碼特殊字符

這是我的課

[ Serializable, XmlRoot("ns1:BatchChanges") ] 
    public class JTDChanges 
    { 
     [XmlElement("OrgUnitChanges")] 
     public List<OrgUnitStage> CustomerChanges = new List<OrgUnitStage>(); 
    } 

任何人都可以請建議我如何才能避免編碼?

回答

0

我相信你正在尋找Xml Namespace functions

[Serializable, XmlRoot("BatchChanges, Namespace = "http://www.w3.org/XML/2008/xsdl-exx/ns1") ] 
public class JTDChanges 
{ 
    [XmlElement("OrgUnitChanges")] 
    public List<OrgUnitStage> CustomerChanges = new List<OrgUnitStage>(); 
} 

現在,在這之前真的有效果,你還需要告訴你的序列化使用此命名空間

// Create a name space prefix 
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
ns.Add("ns1", "ttp://www.w3.org/XML/2008/xsdl-exx/ns1"); 

// Create a serializer 
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(this.GetType()); 

// And pass the namespace along as param 
ser.Serialize(writer, this, ns) 

至於測試你可以聲明以下內容

[XmlElement(ElementName = "point", Namespace = "http://www.w3.org/XML/2008/xsdl-exx/ns1")] 

這將導致 <ns1:point>(whatever the values were you declared it upon)</ns1:point>

+0

謝謝,工作。但它也將我所有從OrgUnitChanges獲得的xml節點都映射到ns1:OrgUnitChanges。我想ns1只附加到根節點。 –

+0

@SriHarsha在這裏有很多職位[這解釋你如何做到這一點](http://stackoverflow.com/questions/28571394/serialize-part-of-xml-file-want-namespace-on-root - 不是-ON-系列化-子元素)。如果這個答案幫助你不要忘記接受它作爲回答/投票給其他人看 –