Search code examples
c#xmlxamlxpathcdata

Processing large CDATA section in C#


I am trying to retrieve a cdata section from an xml document, the format of the xml is like this:

<Configuration>
    <ConfigItem>
        <Key>Hello World</Key>
        <Value><![CDATA[For the value we have a large chunk of XAML stored in a CDATA section]]></Value>
    </ConfigItem>
</Configuration>

What I am trying to do is retrieve the XAML from the CDATA section, my code so far is as follows:

XmlDocument document = new XmlDocument();
document.Load("Configuration.xml");

XmlCDataSection cDataNode = (XmlCDataSection) document.SelectSingleNode("//*[local-name()='Value']").ChildNodes[0];

String cdata = cDataNode.Data;

However the cdata string has been truncated and is incomplete, I guess because the actual cdata is too large to fit in the string object.

Whats the correct way to do this?

EDIT:

So my original assumption that the string was too long was incorrect. The problem now is that my CDATA contains a nested CDATA within it. Reading online it appears that the proper way to escape the nested cdata is to use ]]]]><![CDATA[> which this xml is using, but it seems like when I select the node it is escaping at the wrong place.


Solution

  • When there's nested CDATA sections, what you need to do is piece the data back together. At present, you're just selecting ChildNodes[0] and ignoring all of the other children. What you'll probably find is that ChildNodes[1] contains some plain text, and then ChildNodes[2] contains another CDATA section, and so on.

    You need to extract all of these, extract the data from the CData sections, and concatenate them all together to get the effective "text" contents of the Value element.