0

I've following decimal entities 	 and 
 for TAB and NEWLINE characters in attributes in XML file. After processing the file and saving, above decimal entities are replaced with its hexadecimal equivalents i.e 	 and 
 respectively.

Content in file before processing,

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

Content after processing the file,

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

XmlWriter code block i used,

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

Note: I'm aware that having either decimal or hexadecimal entity in XML file, both case should work. But i'm specifically looking for solutions to retain the same decimal entities after processing the file.

any help one this is much appreciated.

  • 1
    Some XML tools give you a choice of how data is serialized (e.g. whether to use decimal or hexadecimal character references in the output). But I don't know of any tools, other than editors, that allow you to keep the input form unchanged: that's because the input is almost invariably parsed using a generic XML parser, and generic XML parsers don't report such details to the application. That is to say, your `XmlDataDocument` doesn't contain any information about how the characters were represented in the input, so the `XmlWriter` has to "do its own thing". – Michael Kay Jan 25 '21 at 08:55
  • @MichaelKay thanks for the response. That makes sense. Any idea apart from XmlDataDocument any other library in C# that can be used, that'll preserve these character entities – Sarath Kumar Harikrishnan Jan 25 '21 at 09:38
  • 2
    As I say the XML parser doesn't preserve character references so any such library would need to include its own XML parser. Basically, you need to rethink your design: you shouldn't have applications that break if the low-level representation of the XML changes. – Michael Kay Jan 25 '21 at 11:26

0 Answers0