How to update text inside CDATA

I want to replace the text inside the cdata section, but when I just try to add text to it, I lose the CDATA definition.

I have XML like this:

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

      

When I try to update this field with a new value:

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

      

Exit

<title>string</title>    

      

How can I save it as CDATA?

+1


a source to share


2 answers


The title element will have a child CData, which should be different like this: -



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

      

+2


a source


I would do:

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

      



This way you don't have to deal with the CDATA format in your code (change: hardcoding <! [CDATA [doesn't work anyway as Anthony pointed out)

+1


a source