如何避免在xmlroot中编码特殊字符

时间:2015-10-20 12:58:32

标签: c# xml xml-serialization xmlserializer

我尝试使用不同的xmlrootname序列化JTDChanges" ns1:BatchChanges"但在我将其写入文件后进行序列化后," ns1:BatchChanges"被编码为" ns1_x003A_BatchChanges"。

这是我的班级

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

任何人都可以建议我如何避免编码?

1 个答案:

答案 0 :(得分: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>