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
XmlWriterimplementation internal toXmlDocument.The issue disappears if you create an
XmlWriteryourself - the unsupported character will be correctly encoded as an entity reference. ThisXmlWriteris a different (and newer) implementation that sets anEncoderFallbackthat 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
XDocumentAPI, it's much nicer to work with than the old creakyXmlDocumentAPI. And its version ofSavedoesn't have this problem, either!