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 Text
After Saving XML File
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
The issue here seems to be the handling of encoding of entity references by the specific
XmlWriter
implementation internal toXmlDocument
.The issue disappears if you create an
XmlWriter
yourself - the unsupported character will be correctly encoded as an entity reference. ThisXmlWriter
is a different (and newer) implementation that sets anEncoderFallback
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.As an aside, I'd recomment using the LINQ to XML
XDocument
API, it's much nicer to work with than the old creakyXmlDocument
API. And its version ofSave
doesn't have this problem, either!