Question

I have the following XML

<?xml version="1.0"?>
<DisplayViewHtml>
    <embeddedHTML>&lt;![CDATA[&lt;html&gt;&lt;body&gt;&lt;div&gt;Hello World&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;]]&gt;</embeddedHTML>
    <executive>Madan Mishra</executive>
    <imgSRC>/executive/2.jpg</imgSRC>
</DisplayViewHtml>

In the c# code trying to extract the value of embeddedHTML with out CDATA.
My c# code is given below,

XElement displayViewHtml=null;
XmlReader reader = XmlReader.Create(new StringReader(e.Result));
displayViewHtml = XElement.Load(reader);
IEnumerable<XElement> settings = from item in displayViewHtml.Elements() select item;
foreach (XElement setting in settings)
{
    switch (setting.Name.ToString())
    {
        case "embeddedHTML":
            counterViewHtml = setting.Value;
            break;
        case "executive":
            executive = setting.Value;
            break;
        case "imgSRC":
            imgSRC = setting.Value;
            break;
        default:
            //log
            break;
    }
}

from the above code I am able to extract the value of embeddedHTML,executive and imgSRC But embeddedHTML gives

<![CDATA[<html><body><div>Hello World</div></body></html>]]>

but I want

<html><body><div>Hello World</div></body></html>

kindly don't suggest to use .Replace method

Was it helpful?

Solution

As @CamBruce suggested, the problem is that your xml file has encoded characters where they shouldn't. Ideal solution is to fix the program that generate xml file. Anyway if you, for some reason expect a work-around here, this way will do :

.....
case "embeddedHTML":
            var element = XElement.Parse("<embeddedHtml>" + 
                                            setting.Value + 
                                         "</embeddedHtml>");
            counterViewHtml = element.Value;
            break;
.....

The codes above tell the program to create new XElement (which is variable element) by parsing string that already unescaped. Hence, value of newly created XElement will contains string that you want :

<html><body><div>Hello World</div></body></html>

OTHER TIPS

It looks like the CData declaration in the XML is encoded with the rest of the HTML. Ensure that the producer of this XML has the non-encoded CData declaration, like this <![CDATA[ encoded HTML content ]]>

Otherwise, the code you have looks correct. There is nothing special you need to do to read CData with Linq to XML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top