Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing CDATA tag from XmlNode

I have an XmlNode which represents the following xml for example:

XmlNode xml.innerText =
<book>
<name><![CDATA[Harry Potter]]</name>
<author><![CDATA[J.K. Rolling]]</author>
</book>

I want to change this node so that it'll contain the following:

XmlNode xml.innerText =
<book>
<name>Harry Potter</name>
<author>J.K. Rolling</author>
</book>

Any ideas?
Thanks!

like image 780
Niv Avatar asked Aug 04 '13 12:08

Niv


2 Answers

well, if it's exactly how you put it, then it's easy:

xml.innerText = xml.innerText.Replace("![CDATA[","").Replace("]]","");
xmlDoc.Save();// xmlDoc is your xml document
like image 148
No Idea For Name Avatar answered Sep 20 '22 07:09

No Idea For Name


I suggest you to read your entire xml and rewrite it. You can read values without cdata like this

foreach (var child in doc.Root.Elements())
    {
         string name = child.Name;
         string value = child.Value
    }
like image 29
Ehsan Avatar answered Sep 20 '22 07:09

Ehsan