How to avoid encoding of special characters in xmlroot

社会主义新天地 提交于 2019-12-02 18:41:14

问题


I trying to serialize a class JTDChanges with different xmlrootname "ns1:BatchChanges" but after serializing when I write it into a file, "ns1:BatchChanges" is encoded to "ns1_x003A_BatchChanges".

This is my class

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

Can anyone please suggest how can I avoid the encoding?


回答1:


I believe you are looking for the 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>();
} 

Now before this really has a effect, you also need to tell your serializer to use this namespace

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

As for a test you could declare the following

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

which would result in <ns1:point>(whatever the values were you declared it upon)</ns1:point>



来源:https://stackoverflow.com/questions/33237316/how-to-avoid-encoding-of-special-characters-in-xmlroot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!