NET Framework 中是否有任何内置函数可以将字符串编码为有效的 XML unicode?
检查 Xps 文件后,我注意到 Xps 文件中的字符串 <>
被转换为 <>
那么是否有任何内置的.Net 框架中的函数可以为我完成这项工作吗? 如果它不存在,我应该在 myOwn 函数中转义除 <>
之外的哪些字符?
我尝试在 xps 文件中实现搜索,但搜索 <>
而不是 <>
不会返回任何内容。
更新:至少我发现此处的 xml 文档转义字符列表
After checking an Xps file i noticed that the string within the Xps file <>
is converted to <>
So is there any built-in function in the .Net framework that could do this job for me?
If it does not exist what characters becides <>
should i escape in myOwn function?
I try to implement a search within an xps file, but searching for <>
instead of <>
returns nothing.
UPDATE: At least i found the list here of xml document escape characters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XMLTextWriter
是什么你正在寻找。您应该避免使用任何 HTMLEncode 方法(有多种),除非您实际上对文本进行编码以便在 HTML 文档中使用。如果您要对文本进行编码以便在 XML 文档(包括 XHTML)中使用,则应该使用 XMLTextWriter。像这样的东西应该可以解决问题:
另请参阅 其他 stackoverflow 讨论。
XMLTextWriter
is what you're looking for. You should avoid using any of the HTMLEncode methods (there are several) unless you're actually encoding your text for use in an HTML document. If you're encoding text for use in an XML document (including XHTML), you should use XMLTextWriter.Something like this should do the trick:
See also this other stackoverflow discussion.
HtmlEncode 应该适合您。
<
和>
不是 unicode。它们只是转义的 HTML/XML 字符。编辑:
想想看,您可以推出自己的 XmlEncode 方法,因为只有 5 个 预定义实体 XML。 HTML 中有 252 个。
HtmlEncode should work for you.
<
and>
is NOT unicode. They're just escaped HTML/XML characters.Edit:
Come to think of it you could roll your own XmlEncode method since there are only 5 predefined entities in XML. In HTML there are 252.
虽然不是内置的,但微软已经发布了一个反跨站脚本库,应该会有所帮助。它添加了 .NET 的 Html.Encode 方法之外的新功能,包括 XML 编码方法。
教程
下载
Not built in but Microsoft have released an anti-cross site scripting library that should help. It adds new features beyond .NET's Html.Encode method, including an XML encoding method.
Tutorial
Download