Encoding XML in C#

2019-07-18 03:46发布

问题:

I have a xml file which has umlauts in it like so:

<NameGe>ËÇ</NameGe>

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

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

so basically it should write the numeric/encoded value of the umlaut.

Regards.

回答1:

You can do it by overriding WriteString of 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));
    }
}


回答2:

Use HttpUtility.HtmlEncode and HttpUtility.HtmlDecode.



标签: c# encoding