C#XML避免使用XDocument进行html编码 [英] c# XML avoid html encode using XDocument

查看:82
本文介绍了C#XML避免使用XDocument进行html编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XDocument创建xml文件后,我最终得到:

After creating an xml file using XDocument I end up with:

<![CDATA[text]]>

<br />

但是我想将它们保留为HTML,如何停止呢?

But I want to keep these as HTML, how do I stop this?

推荐答案

我假设您要将字符串传递到某个地方的XDocument或XElement构造函数,该字符串包含XML.别.而是使用XDocument.Parse(string)XElement.Parse(string).

I assume you are passing a string to an XDocument or XElement constructor somewhere, where that string contains XML. Don't. Instead, use XDocument.Parse(string) or XElement.Parse(string).

请参见 http://msdn.microsoft .com/en-us/library/system.xml.linq.xdocument.parse.aspx 请注意,这显然仅适用于恰好是格式正确的XML的HTML.

Note that this will only work for HTML that happens to be well-formed XML, obviously.

例如:

XElement.Parse("<TagName>The string has <br /> in it.</TagName>")

如果您静态地知道文本,则只需使用构造函数调用即可构建文本,例如

If you statically know the text, just build it up using constructor calls, e.g.

new XElement("TagName", "The string has ", new XElement("br"), " in it.")

这篇关于C#XML避免使用XDocument进行html编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆