c# XML avoid html encode using XDocument

帅比萌擦擦* 提交于 2019-12-24 02:59:34

问题


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

<![CDATA[text]]>

and

<br />

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


回答1:


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).

See http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse.aspx and http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.parse.aspx

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

For example:

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.")


来源:https://stackoverflow.com/questions/19271080/c-sharp-xml-avoid-html-encode-using-xdocument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!