简体   繁体   中英

Use Special Character In Xml Element Name

I'm trying generate xml file from string. One element has special characters in name and output should look like this:

<Discount %>10</Discount %>

I know it's not allowed but does "not allowed" mean that it's completely impossible or is it just a bad practice?

If it's possible how can I achieve it?

Here's my code

StringBuilder stringBuilder = new StringBuilder();
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder))
{
    xmlWriter.WriteStartElement("Client");

    xmlWriter.WriteElementString("Discount %", client.DiscountPercent.ToString());

    xmlWriter.WriteEndElement();
    xmlWriter.Flush();

    XmlDocument outputXmlDocument = new XmlDocument();
    outputXmlDocument.LoadXml(stringBuilder.ToString()); // throws this: System.Xml.XmlException: expected '>' (3E) but found '%' (25)  Line 1, position 618.
    File.WriteAllText(path, outputXmlDocument.SelectSingleNode("Tables").OuterXml);
}

The element name must be in a single word.

and % should be avoided

Consider :

<Discount>  
<DiscountPercent>  
<Discount_Percent> 

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