Saving XML file from one location to another location using XML DOCUMENT

898 Views Asked by At

While saving the existing XML to new location, entities escaped from the content and replaced with Question Mark

See the snaps below entity ‐ (- as Hex) present while reading but its replaced with question mark after saving to another location.

While Reading as Inner XML

While Reading as inner XML

While Reading as Inner Text

While Reading as inner Text

After Saving XML File

After Saving XML

EDIT 1 Below is my code

string path = @"C:\work\myxml.XML";
string pathnew = @"C:\work\myxml_new.XML";
//GetFileEncoding(path);
XmlDocument document = new XmlDocument();
XmlDeclaration xmlDeclaration = document.CreateXmlDeclaration("1.0","US-ASCII",null);
//document.CreateXmlDeclaration("1.0", null, null);
document.Load(path);
string x = document.InnerText;
document.Save(pathnew);

EDIT 2 My source file looks like below. I need to retain the entities as it is

enter image description here

1

There are 1 best solutions below

6
On BEST ANSWER

The issue here seems to be the handling of encoding of entity references by the specific XmlWriter implementation internal to XmlDocument.

The issue disappears if you create an XmlWriter yourself - the unsupported character will be correctly encoded as an entity reference. This XmlWriter is a different (and newer) implementation that sets an EncoderFallback that encodes characters as entity references for characters that can't be encoded. Per the remarks in the docs, the default fallback mechanism is to encode a question mark.

var settings = new XmlWriterSettings
{
    Indent = true,
    Encoding = Encoding.GetEncoding("US-ASCII")
};

using (var writer = XmlWriter.Create(pathnew, settings))
{
    document.Save(writer);            
}

As an aside, I'd recomment using the LINQ to XML XDocument API, it's much nicer to work with than the old creaky XmlDocument API. And its version of Save doesn't have this problem, either!