jpkeisala
jpkeisala

Reputation: 8946

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?

Upvotes: 1

Views: 2764

Answers (2)

Matthew Flaschen
Matthew Flaschen

Reputation: 285047

I would do:

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

That way you don't have to deal with the CDATA format in your code (edit: hard-coding <![CDATA[ doesn't work anyway, as pointed out by Anthony)

Upvotes: 1

AnthonyWJones
AnthonyWJones

Reputation: 189535

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

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

Upvotes: 2

Related Questions