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

How to avoid encoding of special characters in xmlroot

我试图用不同的 xmlrootname "ns1:BatchChanges" 序列化一个 class JTDChanges,但是当我将它写入文件时序列化后,"ns1:BatchChanges" 被编码为 "ns1_x003A_BatchChanges"。

这是我的class

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

谁能建议我如何避免编码?

我相信您正在寻找 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>