简体   繁体   English

从包含HTML标签的xml中读取CDATA

[英]read CDATA from xml containing HTML tags

I'm trying to read the CDADA of this XML but it's getting ignored. 我正在尝试读取此XML的CDADA,但是它被忽略了。 The trailing "XX" gets read ok. 尾随的“ XX”可以正常读取。 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>");
}

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 我只是跑了下面,item.InnerText是

<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>";
}

At a guess, you're viewing the output in an HTML document in a browser and are looking at the rendered page. 猜测是,您正在浏览器中查看HTML文档中的输出,并正在查看呈现的页面。

The <link> tag (being a tag) is therefore not rendered. 因此,不会呈现<link>标签(作为标签)。

Use the browser's View → Source feature to see it. 使用浏览器的查看→源功能进行查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM