Handling US-ASCII encoded XML with unsupported entity reference

依然范特西╮ 提交于 2019-12-12 03:08:54

问题


This question is the continuation of this page

PROCESS: The process involved, Opening XML file and do some modification in specific nodes and save it back to another location.

PROBLEM FACING: While Saving after some modifications in XML, unsupported entity references like ö converted into ö. I want to retain the entity as it is in the source (ö)

As ö and ö are same character but i need to retain as it is in source xml.

XML SOURCE

<?xml version="1.0" encoding="US-ASCII"?>
<heads>
    <head type="TRANSFER">
        <headtext xml:lang="ENG" original="y">My Name &#x00F6;is Sinthiya</headtext>
    </head>
</heads>

EXPECTED OUTPUT

<?xml version="1.0" encoding="US-ASCII"?>
<heads>
    <head type="TRANSFER">
        <headtext xml:lang="ENG" original="y">My Name &#x00F6;is Sinthiya</headtext>
    </head>
</heads>

GETTING RIGHT NOW

<?xml version="1.0" encoding="US-ASCII"?>
<heads>
    <head type="TRANSFER">
        <headtext xml:lang="ENG" original="y">My Name &#xF6;is Sinthiya</headtext>
    </head>
</heads>

My Code

string path = @"C:\work\myxml.XML";
string pathnew = @"C:\work\myxml_new.XML";
XmlDocument doc = new XmlDocument();
doc.Load(path);
using (var writer = XmlWriter.Create(pathnew, new XmlWriterSettings { Indent= true, Encoding = Encoding.ASCII }))
{
    doc.Save(writer);
}

来源:https://stackoverflow.com/questions/37160364/handling-us-ascii-encoded-xml-with-unsupported-entity-reference

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