arsena
arsena

Reputation: 1975

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);
}

Upvotes: 0

Views: 1919

Answers (1)

Emmanuel DURIN
Emmanuel DURIN

Reputation: 4913

The element name must be in a single word.

and % should be avoided

Consider :

<Discount>  
<DiscountPercent>  
<Discount_Percent> 

Upvotes: 1

Related Questions