Search code examples
c#.netcdataxmlnode

How do I update text inside CDATA


I want to replace text inside cdata section but when I simply trying to add text to it I lose CDATA definition.

I have a XML like this:

<title><![CDATA[string]]></title>

When I try to update this field with new value:

myXmlNode.SelectSingleNode("title").InnerText = TextBoxName.Text;

Output is

<title>string</title>    

How do can I keep it as CDATA?


Solution

  • The title element will have an CData child which needs to be cast like so:-

     ((XmlCDataSection)myXmlNode.SelectSingleNode("title").FirstChild).Value = TextBoxName.Text