if (c == 0) {
throw new IOException("Invalid null character in text to output");
}
if (c < ' ' || (c >= 0x7F && c <= 0x9F)) {
String msg = "Invalid white space character (0x" + Integer.toHexString(c) + ") in text to output";
if (mXml11) {
msg += " (can only be output using character entity)";
}
throw new IOException(msg);
}
if (c > 0x10FFFF) {
throw new IOException("Illegal unicode character point (0x" + Integer.toHexString(c) + ") to output; max is 0x10FFFF as per RFC");
}
/*
* Surrogate pair in non-quotable (not text or attribute value) content, and non-unicode encoding (ISO-8859-x,
* Ascii)?
*/
if (c >= SURR1_FIRST && c <= SURR2_LAST) {
throw new IOException("Illegal surrogate pair -- can only be output via character entities, which are not allowed in this content");
}
throw new IOException("Invalid XML character (0x"+Integer.toHexString(c)+") in text to output");
ampersand (&) is escaped to &
double quotes (") are escaped to "
single quotes (') are escaped to '
less than (<) is escaped to <
greater than (>) is escaped to >
string xml = "<node>it's my \"node\" & i like it 0x12 x09 x0A 0x09 0x0A <node>";
string encodedXml1 = System.Security.SecurityElement.Escape(xml);
string encodedXml2= System.Net.WebUtility.HtmlEncode(xml);
encodedXml1
"<node>it's my "node" & i like it 0x12 x09 x0A 0x09 0x0A <node>"
encodedXml2
"<node>it's my "node" & i like it 0x12 x09 x0A 0x09 0x0A <node>"
15 回答
唯一的非法字符是
&
,<
和>
(以及属性中的"
或'
) .它们使用XML entities进行转义,在这种情况下,您希望
&
为&
.但实际上,你应该使用一个为你编写XML的工具或库,并为你抽象出这种东西,这样你就不用担心了 .
好的,让我们将(1)任何XML文档中完全无效的字符和(2)需要转义的字符分开:
@dolmen Invalid Characters in XML提供的答案仍然有效,但需要使用XML 1.1规范进行更新 .
1.无效字符
此处描述的字符是允许插入XML文档的所有字符 .
1.1.在XML 1.0中
允许字符的全局列表是:
基本上,不允许使用Unicode范围之外的控制字符和字符 . 这也意味着禁止调用例如字符实体

.1.2.在XML 1.1中
允许字符的全局列表是:
XML修订版的这一修订扩展了允许的字符,因此允许控制字符,并考虑到Unicode标准的新修订版,但仍然不允许使用这些版本: NUL (x00) , xFFFE , xFFFF ...
但是,不鼓励使用控制字符和未定义的Unicode char .
还可以注意到,所有解析器并不总是考虑到这一点,并且可能拒绝具有控制字符的XML文档 .
2.需要转义的字符(以获取格式正确的文档):
<
必须使用<
实体进行转义,因为它被假定为标记的开头 .&
必须使用&
实体进行转义,因为它假定为实体引用的开头应使用
>
实体转义>
. 它不是强制性的 - 它取决于上下文 - 但强烈建议逃避它 .'
应该使用'
实体进行转义 - 在单引号中定义的属性中必须使用 - 但强烈建议始终对其进行转义 ."
应该使用"
实体进行转义 - 在双引号内定义的属性中是必需的,但强烈建议始终将其转义 .有效字符列表位于XML specification中:
这是一个C#代码,用于从字符串中删除XML无效字符并返回新的有效字符串 .
预先声明的字符是:
http://xml.silmaril.ie/specials.html
另一种在C#中逃避可能不需要的XML / XHTML字符的简单方法是:
除了potame的答案,如果你想要使用CDATA块逃脱 .
如果你把你的文字 in a CDATA block then you don't need to use escaping . 在这种情况下,您可以使用 all characters in the following range :
注意:除此之外,您不能使用
]]>
字符序列 . 因为它会匹配CDATA块的末尾 .如果仍然存在无效字符(例如控制字符),则可能最好使用某种编码(例如base64) .
使用XmlConvert.IsXmlChar Method删除C#中不正确的XML字符的另一种方法(自.NET Framework 4.0起可用)
或者您可以检查所有字符是否都是XML有效的 .
.Net小提琴 - https://dotnetfiddle.net/v1TNus
例如,垂直选项卡symbol(\ v)对XML无效,它是有效的UTF-8,但不是有效的XML 1.0,甚至许多库(包括libxml2)都会错过它并静默输出无效的XML .
这个答案对我有用
详情link to Blog
对于Java人员,Apache有一个实用程序类(StringEscapeUtils),它有一个辅助方法escapeXml,可用于使用XML实体转义字符串中的字符 .
在Woodstox XML处理器中,无效字符按此代码分类
来自here
在C#中,使用System.Security.SecurityElement.Escape或System.Net.WebUtility.HtmlEncode来转义这些非法字符 .
有人试过这个
System.Security.SecurityElement.Escape(yourstring)
?这将用有效的等效字符替换字符串中的无效XML字符总之,文本中的有效字符是:
标签,换行和回车;
所有非控制字符均有效 except &和<;
如果遵循]],则
XML规范的2.2和2.4节提供了详细的答案:
Characters
Character data
对于XSL(在非常懒惰的日子里),我使用:
翻译所有未签署的&-signs;适当的 .
我们有输入在CDATA中的情况,但使用XML的系统不考虑它 . 这是一个草率的修复,要小心......