Handling US-ASCII encoded XML with unsupported entity reference

225 Views Asked by At

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);
}
0

There are 0 best solutions below