问题描述
对于给定的的XmlElement
,我需要能够设置内部文本的unicode字符串的转义版本,尽管UTF-8最终被编码的文件。有没有实现这一目标。
For a given XmlElement
, I need to be able to set the inner text to an escaped version of the unicode string, despite the document ultimately being encoded in UTF-8. Is there any way of achieving this?
下面是代码的简单版本的任何方式:
Here's a simple version of the code:
const string text = "ñ";
var document = new XmlDocument {PreserveWhitespace = true};
var root = document.CreateElement("root");
root.InnerXml = text;
document.AppendChild(root);
var settings = new XmlWriterSettings {Encoding = Encoding.UTF8, OmitXmlDeclaration = true};
using (var stream = new FileStream("out.xml", FileMode.Create))
using (var writer = XmlWriter.Create(stream, settings))
document.WriteTo(writer);
预期:
Expected:
<root>ñ</root>
实际
Actual:
<root>ñ</root>
使用的XmlWriter
直接调用 WriteRaw(文本)
工作,但我只是有机会获得的XmlDocument
和序列化后发生的。在的XmlElement
,的InnerText
脱&安培;
到&放大器;放大器;
,符合市场预期,并设置值
抛出异常。
Using an XmlWriter
directly and calling WriteRaw(text)
works, but I only have access to an XmlDocument
, and the serialization happens later. On the XmlElement
, InnerText
escapes the &
to &
, as expected, and setting Value
throws an exception.
有一个的XmlElement
的内部文本设置为逃脱ASCII文本,而不管最终使用的编码的一些方法?我觉得我必须缺少明显的东西,或者它只是不可能的。
Is there some way of setting the inner text of an XmlElement
to the escaped ascii text, regardless of the encoding that is ultimately used? I feel like I must be missing something obvious, or it's just not possible.
感谢
推荐答案
如果你问的XmlWriter生成ASCII输出,它应该给所有非ASCII内容你人品引用。
If you ask XmlWriter to produce ASCII output, it should give you character references for all non-ASCII content.
var settings = new XmlWriterSettings {Encoding = Encoding.ASCII, OmitXmlDeclaration = true};
输出仍然是有效的UTF-8,因为ASCII是UTF-8的一个子集。
The output is still valid UTF-8, because ASCII is a subset of UTF-8.
这篇关于转义的XmlElement unicode字符串,尽管在UTF-8编写XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!