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

旧时模样 提交于 2020-01-16 19:33:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!