XML Encoding in C #

I have an xml file that has umlauts in it like this:

<NameGe>ËÇ</NameGe> 

Is there a way to read this file and write it like this:

 <NameGe>&#214;&#231;</NameGe> 

so basically he should write a numerical / encoded umlaut value.

Sincerely.

+2
source share
3 answers

You can do this by overriding WriteString from XmlTextWriter

 MemoryStream m = new MemoryStream(); MyWriter xmlWriter = new MyWriter(m); XDocument xDoc = XDocument.Parse(xml); xDoc.Save(xmlWriter); xmlWriter.Flush(); string s = Encoding.UTF8.GetString(m.ToArray()); 

-

 public class MyWriter : XmlTextWriter { public MyWriter(Stream s) : base(s,Encoding.UTF8) { } public override void WriteString(string text) { base.WriteRaw(HttpUtility.HtmlEncode(text)); } } 
+4
source

Use HttpUtility.HtmlEncode and HttpUtility.HtmlDecode .

+1
source

You cannot encode it using only XML. At least XML 1.0 does not support characters like I know. You need to parse this XML, encode specific values ​​using HttpUtility.HtmlEncode or convert it to base64 and write the XML back to the file.

+1
source

All Articles