Xdocument - 如何转换非 html 安全字符
我的 UTF-8 xml 元素内有一个“title”属性,例如
<tag title="This is some test with special chars §£" />
,因为我希望该属性的内容直接打印在 HTML 页面中,所以我尝试获得如下输出:
<tag title="This is some test with special chars §£" />
我添加的代码片段有属性看起来像这样:
new XElement( "tag",
new XAttribute( "title" , title)
);
字符如 &和 " 被转义,但 §£ 没有 - 因为它们是有效的 utf-8 字符。 我应该改变什么?
I have a "title" attribute inside elements of my UTF-8 xml, e.g.
<tag title="This is some test with special chars §£" />
as I want the content of this attribute to be printed directly in an HTML page, I'm trying to have an output like:
<tag title="This is some test with special chars §£" />
The code fragment where I add there attribute looks like this:
new XElement( "tag",
new XAttribute( "title" , title)
);
Characters such as & and " are escaped, but §£ are not - as they're valid utf-8 characters.
What should I change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果页面声明为 UTF-8,则 HTML 中支持 UTF-8 字符。
如果页面的默认编码是范围较小的字符集,则它将无法正确呈现所有 UTF-8 字符。但是,如果文档声明为 UTF-8,它们应该可以正常显示。
您可能需要显式声明您的页面为 UTF-8。
有多种方法可以执行此操作:
UTF-8 characters are supported in HTML, if the page is declared as UTF-8.
If the default encoding for the page is a character set with a smaller range, then it will not render all of the UTF-8 characters properly. However, if the document is declared as UTF-8 they should display fine.
Rather than replacing characters with entity references, you may need to explicitly declare the encoding of your page as UTF-8.
There are a variety of ways to do this:
<meta charset="UTF-8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<?xml version="1.0" encoding="UTF-8"?>
也许您可以手动解码这些字符。我以前用过这个
请参阅此 HTMLSymbol 表 了解更多信息
May be you can manually decode those characters. I have used this before
Refer this HTMLSymbol table for more info