Search code examples
c#xmlcdata

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?


Solution

  • 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