问题
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
回答1:
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!
来源:https://stackoverflow.com/questions/37140115/saving-xml-file-from-one-location-to-another-location-using-xml-document