简体   繁体   中英

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:

<![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.

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.

Use XmlNode.InnerText and the CDATA will work.

If you must use InnerXml... you can omit the CDATA tags and it will also work.

If you need both InnerXml and CDATA, I don't have an answer... or a scenario where the two would be used together

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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