如何与XDocument.Save编写XML时,我preserve特殊字符()? [英] How do I preserve special characters when writing XML with XDocument.Save()?

查看:230
本文介绍了如何与XDocument.Save编写XML时,我preserve特殊字符()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML源中有版权字符作为&放大器;#x00A9; 。当与此code编写XML:

My source XML has the copyright character in it as ©. When writing the XML with this code:

var stringWriter = new StringWriter();
segmentDoc.Save(stringWriter);
Console.WriteLine(stringWriter.ToString());

这是渲染,版权字符作为一个小C与它周围的一圈。我想preserve原来的code所以它被回吐出来为&放大器;#x00A9; 。我怎样才能做到这一点?

it is rendering that copyright character as a little "c" with a circle around it. I'd like to preserve the original code so it gets spit back out as ©. How can I do this?

更新:??我也注意到,在源声明如下< XML版本=1.0编码=UTF-8> 但是我保存的输出看起来像< XML版本=1.0编码=UTF-16> 。我可以表明我所要的输出仍然是UTF-8?但愿解决它?

Update: I also noticed that the source declaration looks like <?xml version="1.0" encoding="utf-8"?> but my saved output looks like <?xml version="1.0" encoding="utf-16"?>. Can I indicate that I want the output to still be utf-8? Would that fix it?

UPDATE2:此外,&放大器;#x00A0; 越来越为输出y 。我绝对不希望这种事情发生!

Update2: Also, &#x00A0; is getting output as ÿ. I definitely don't want that happening!

UPDATE3:&放大器;#x00A7; 正在成为一个小盒子,这是错误的,太。它应该是§

Update3: &#x00A7; is becoming a little box and that is wrong, too. It should be §

推荐答案

我强烈怀疑你将不能够做到这一点。从根本上说,版权符号的&放大器;#x00A9; - 它们是同一事物的不同重新presentations,和我预期在内存中重新presentation标准化这一点。

I strongly suspect you won't be able to do this. Fundamentally, the copyright sign is &#x00A9; - they're different representations of the same thing, and I expect that the in-memory representation normalizes this.

你在做什么与事后XML?任何理智的应用程序处理生成的XML应该与它的罚款。

What are you doing with the XML afterwards? Any sane application processing the resulting XML should be fine with it.

您的可以的能够说服它使用实体引用,如果你明确地连接$ C C $它用ASCII ...但我不知道。

You may be able to persuade it to use the entity reference if you explicitly encode it with ASCII... but I'm not sure.

编辑:你绝对可以使其使用不同的编码。你只需要一个的StringWriter 该报告说,它的原生的编码是UTF-8。这里有一个简单的类,你可以使用如下:

You can definitely make it use a different encoding. You just need a StringWriter which reports that its "native" encoding is UTF-8. Here's a simple class you can use for that:

public class Utf8StringWriter : StringWriter
{
    public override Encoding Encoding
    {
         get { return Encoding.UTF8; }
    }
}

您可以尝试改变它使用 Encoding.ASCII 还有,看看是什么一样的版权标记...

You could try changing it to use Encoding.ASCII as well and see what that does to the copyright sign...

这篇关于如何与XDocument.Save编写XML时,我preserve特殊字符()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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