编码XML在C# [英] Encoding XML in C#

查看:150
本文介绍了编码XML在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有变音符号在里面,像这样一个XML文件:

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.

问候。

推荐答案

您可以通过覆盖做到 WriteString 的XmlTextWriter

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

这篇关于编码XML在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆