Removing CDATA tag from XmlNode
Asked Answered
C

2

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!

Colewort answered 4/8, 2013 at 12:3 Comment(4)
is their any specific need of removing tags or you just want to read values? And by the way always post the correct sample. You are missing closing cdata tags in your sample xml.Zurn
There is, It is passed to a clientColewort
who is creating the xml?Zurn
I am not a Harry Potter fan, but seeing J.K. Rowling's name misspelled breaks my heart.Prier
B
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
Betrothal answered 4/8, 2013 at 12:28 Comment(4)
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 wellColewort
@Colewort 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 infoBetrothal
This wound up working, just with a tiny modification : instead of innerText, I needed to use innerXML. Tnx!Colewort
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: msdn.microsoft.com/en-us/library/…Woehick
Z
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
    }
Zurn answered 4/8, 2013 at 12:28 Comment(2)
It's possible that the XML is supplied from a third party and needs to be cleaned up.Outpouring
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. Also, Gnome and CDATA also.Figured

© 2022 - 2024 — McMap. All rights reserved.