0

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);
}
Community
  • 1
  • 1
Karthick Gunasekaran
  • 2,697
  • 1
  • 15
  • 25
  • As I said in your [previous question](http://stackoverflow.com/questions/37140115/saving-xml-file-from-one-location-to-another-location-using-xml-document), there's no easy fix for this. Even overriding parts of `XmlWriter` to ensure 4 chars may result in others being 'wrong'. As they are semantically the same, what reason do you have for requiring they are lexicographically the same? – Charles Mager May 11 '16 at 10:54
  • @CharlesMager, As per the instruction given by client, Entity references should be retained as it is. I don't want to override. – Karthick Gunasekaran May 11 '16 at 11:11
  • Writing non-normalized XML with standard libraries is very hard - would not recommend it to anyone. Putting price tag (i.e. "10 weeks to track source information in XML to keep de-normalized XML untouched during editing") would be good *starting point* for conversation with your "client". – Alexei Levenkov May 11 '16 at 16:32
  • @AlexeiLevenkov, Thanks. is there any other way to get solution without any libraries? – Karthick Gunasekaran May 12 '16 at 06:24

0 Answers0