简体   繁体   中英

How to avoid encoding of special characters in xmlroot

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM