Removing CDATA tag from XmlNode

2019-04-08 03:34发布

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!

2条回答
2楼-- · 2019-04-08 04:20

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
查看更多
The star\"
3楼-- · 2019-04-08 04:26

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
    }
查看更多
登录 后发表回答