Encoding XML in C#

2019-07-18 03:39发布

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.

标签: c# encoding
2条回答
乱世女痞
2楼-- · 2019-07-18 03:57

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));
    }
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-18 04:05

Use HttpUtility.HtmlEncode and HttpUtility.HtmlDecode.

查看更多
登录 后发表回答