NET Framework 中是否有任何内置函数可以将字符串编码为有效的 XML unicode?

发布于 2024-09-19 08:28:16 字数 455 浏览 9 评论 0原文

检查 Xps 文件后,我注意到 Xps 文件中的字符串 <> 被转换为 &lt;&gt;

那么是否有任何内置的.Net 框架中的函数可以为我完成这项工作吗? 如果它不存在,我应该在 myOwn 函数中转义除 <> 之外的哪些字符?

我尝试在 xps 文件中实现搜索,但搜索 <> 而不是 &lt;&gt; 不会返回任何内容。

更新:至少我发现此处的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-09-26 08:28:16

XMLTextWriter 是什么你正在寻找。您应该避免使用任何 HTMLEncode 方法(有多种),除非您实际上对文本进行编码以便在 HTML 文档中使用。如果您要对文本进行编码以便在 XML 文档(包括 XHTML)中使用,则应该使用 XMLTextWriter。

像这样的东西应该可以解决问题:

StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.WriteString('Your String Goes here, < and >, as well as other special chars will be properly encoded');
xmlWriter.Flush();

Console.WriteLine("XML Text: {0}", strWriter.ToString());

另请参阅 其他 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:

StringWriter strWriter = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);
xmlWriter.WriteString('Your String Goes here, < and >, as well as other special chars will be properly encoded');
xmlWriter.Flush();

Console.WriteLine("XML Text: {0}", strWriter.ToString());

See also this other stackoverflow discussion.

时间你老了 2024-09-26 08:28:16

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.

離殇 2024-09-26 08:28:16

虽然不是内置的,但微软已经发布了一个反跨站脚本库,应该会有所帮助。它添加了 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文