Preserve decimal character entities in XML C#

233 Views Asked by At

I use XmlDocument class for loading XML and XmlWriter class for generating the file. I wanted to preserve the decimal character entities (characters in bold) that's present in the xml, like the one below,

<car id="wait for the signal &#10;&#10; then proceed">

Tried options like XmlTextReader, but had no luck. After processing the above line in the file looks something like below,

    <car id="wait for the signal

then proceed">

or

<car id="wait for the signal &#xA;&#xA; then proceed">

XmlWriter code block i used,

XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
    Indent = true,
    Encoding = encoding,
    NewLineHandling = NewLineHandling.None
};
XmlDataDocument xmlDataDocument = new XmlDataDocument
{
    PreserveWhitespace = true
};
xmlDataDocument.LoadXml(xmlString);
using (XmlWriter writer = XmlWriter.Create(filepath, xmlWriterSettings))
{
    if (writer != null)
    {
        xmlDataDocument.Save(writer);
    }
}

any help one this is much appreciated.

2

There are 2 best solutions below

0
On

As far as an XML parser is concerned, it will always treat a numeric character reference in exactly the same way as it treats the character itself.

Your only possible way forward is to preprocess the file before the XML parser gets to see it, replacing the & with some other character such as §. And then of course, reverse the process afterwards.

3
On

I am unsure what you are trying to achieve here.

Both &#10; and &#xA; are equivalent. They both reference a newline. One in is decimal form, the other is in hexadecimal form. Either will work with the XML parser.

If you read the official XML Documentation, it specifies that either method of referencing is allowed.

Why do you need to preserve the decimal form?