user441365
user441365

Reputation: 4032

read CDATA from xml containing HTML tags

I'm trying to read the CDADA of this XML but it's getting ignored. The trailing "XX" gets read ok. Why?

<?xml version="1.0" encoding="utf-8" ?>
<data>
  <item key="one"><![CDATA[<link rel="Stylesheet" type="text/css" href="@Url.Content("~/Site.css")" />]]>XX</item>
</data>

this is the code that reads the values:

XmlDocument headdata = new XmlDocument();
headdata.Load(HttpContext.Current.Server.MapPath("~/XML.xml"));

foreach (XmlNode item in headdata.SelectNodes("/data/item"))
{

    HttpContext.Current.Response.Write(item.Attributes["key"].InnerText + ": " +
                                                        item.InnerText + "<BR>");
}

Upvotes: 0

Views: 951

Answers (2)

Justin Pihony
Justin Pihony

Reputation: 67135

I would suggest debugging this and making sure you are loading the values you are expecting. I just ran the below and item.InnerText was

<link rel="Stylesheet" type="text/css" href="blah" />XX

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<data> <item key=\"one\"><![CDATA[<link rel=\"Stylesheet\" type=\"text/css\" href=\"blah\" />]]>XX</item></data>");
foreach (XmlNode item in xmlDoc.SelectNodes("/data/item"))
{
    var x = item.Attributes["key"].InnerText + ": " + item.InnerText + "<BR>";
}

Upvotes: 1

Quentin
Quentin

Reputation: 944443

At a guess, you're viewing the output in an HTML document in a browser and are looking at the rendered page.

The <link> tag (being a tag) is therefore not rendered.

Use the browser's View → Source feature to see it.

Upvotes: 0

Related Questions