简体   繁体   English

在XML CDATA中读取和处理HTML

[英]Reading and Manipulating HTML inside XML CDATA

When I grab HTML from an XML CDATA section, manipulate it (in my case, perform C# string methods on the text within the CDATA section), and then display the edited version... it automatically comments out my first HTML tag: 当我从XML CDATA部分中获取HTML时,对其进行操作(在我的情况下,对CDATA部分中的文本执行C#字符串方法),然后显示编辑后的版本...它将自动注释掉我的第一个HTML标记:

<![CDATA[ <p>Lorem ipsum dolor sit amet...</p> ]]>

converts to this on the page: 在页面上转换为此:

<!--[CDATA[ <p--> Lorem ipsum dolor sit amet... ]]>

which breaks the styling for that first paragraph and renders the closing CDATA tag after the content. 这会破坏第一段的样式,并在内容之后呈现结束的CDATA标记。

How do I fix this? 我该如何解决?

EDIT: I couldn't find anything about this, so I tried a few things for poos and giggles, and this worked: 编辑:我对此一无所获,所以我尝试了一些小便和咯咯笑,这工作:

<![CDATA]> <p>Lorem ipsum dolor sit amet...</p> </]]>

which converted to this on the page: 在页面上转换为:

<!--[CDATA]--> <p>Lorem ipsum dolor sit amet...</p> <!--]]-->

However... I'm not sure if this will affect my page in a negative way? 但是...我不确定这是否会对我的网页产生负面影响?

Is there another way? 还有另一种方法吗?

When using XmlNode.InnerXml on an XML element that contains CDATA... it comments out the first element. 在包含CDATA的XML元素上使用XmlNode.InnerXml时,它将第一个元素注释掉。

Use XmlNode.InnerText and the CDATA will work. 使用XmlNode.InnerText ,CDATA将起作用。

If you must use InnerXml... you can omit the CDATA tags and it will also work. 如果必须使用InnerXml ...,则可以省略CDATA标记,它也将起作用。

If you need both InnerXml and CDATA, I don't have an answer... or a scenario where the two would be used together 如果你需要InnerXml和CDATA,我没有答案......或者两人便一起使用的场景

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM