7

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!

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
Niv
  • 2,294
  • 5
  • 29
  • 41

2 Answers2

9

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
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • Unfortunately, it's not exactly how I put it. It's nested more complicately, and this code ( which I've tried ) seems to remove other major parts as well – Niv Aug 04 '13 at 12:30
  • @Niv can you share more of your code or give us more information? it's string manipulation you want i think, but i don't know what exact manipulation you need. i can help if you have more info – No Idea For Name Aug 04 '13 at 12:35
  • 2
    This wound up working, just with a tiny modification : instead of innerText, I needed to use innerXML. Tnx! – Niv Aug 04 '13 at 12:37
  • 1
    The cdata tags are there for a good reason, to escape invalid xml characters: <,>,',", and &. If you go this route you should escape these characters using something like this: http://msdn.microsoft.com/en-us/library/system.security.securityelement.escape.aspx – dana Aug 04 '13 at 12:49
1

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
    }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • 2
    It's possible that the XML is supplied from a third party and needs to be cleaned up. – Carl Onager Oct 09 '13 at 10:45
  • One reason to remove CDATA is that libxml doesn't handle CDATA. This would require him to take data received from a third party and make it a format that will work or remove it. See [gnome and CDATA](https://mail.gnome.org/archives/xml/2008-September/msg00092.html). Also, [Gnome and CDATA also](http://www.xmlsoft.org/search.php?query=cdata&submit=Search). – Michele Feb 12 '16 at 15:41