Linq to XML查询XDoc

时间:2014-04-25 15:23:02

标签: c# asp.net .net xml linq

我希望能够从span类访问文本,同时还能够获取span类之外的信息。例如:

以下是我的XML文档信息示例:

    <item><title>Operations Applications - MDI Diagnostics</title><link>http://confidential-link.com</link>
<description><![CDATA[<div style="margin-top:5px"><link rel="stylesheet" type="text/css" href="confidential" />
<span class="srch-Icon"><a href="confidential" title="Operations Applications - MDI Diagnostics">
<img src="confidential" alt="Web Page" border="0" /></a></span>
<span class="psrch-Description"> THE INFORMATION I WANT</span>
<p class="srch-Metadata"><span class="srch-URL">
<a href="confidential" title="Operations Applications - MDI Diagnostics">confidential link</a>
        -
        66KB
          </span></p></div>]]></description>
<author>Bob Smith
</author><pubDate>Mon, 10 Mar 2014 18:53:49 GMT</pubDate><search:dotfileextension>.ASPX</search:dotfileextension><search:size>68076</search:size>
<search:hithighlightedsummary> SIMILAR TO THE INFORMATION I WANT, COULD BE OPTION 2  </search:hithighlightedsummary>
</item>

以下是我现在所拥有的:

                var feeds = from feed in xdoc.Descendants("item")
                        select new RSSS
                        {
                            Site = "TOPS",
                            URL = url, 
                            Title = feed.Element("title").Value,
                            Link = feed.Element("link").Value,
                            Description = feed.Element("description").Value

                        };

返回&#34;描述&#34;正如所料:

<div style="margin-top:5px"><link rel="stylesheet" type="text/css" href="confidential" />
<span class="srch-Icon"><a href="confidential" title="Operations Applications - MDI Diagnostics">
<img src="confidential" alt="Web Page" border="0" /></a></span>
<span class="psrch-Description"> THE INFORMATION I WANT</span>
<p class="srch-Metadata"><span class="srch-URL">
<a href="confidential" title="Operations Applications - MDI Diagnostics">confidential link</a>
        -
        66KB
          </span></p></div>

那么如何具体访问&#34; span class = psrch-Description&#34;之间的信息。虽然还能像Link和Title那样访问吗?

**因为我不是在寻找像

这样的东西

var feeds = from feed in xDoc.Descendants("Show") where (string)feed.Attribute("Code") == "456" select new { EventDate = feed.Attribute("Date").Value };

这不允许我获取其他信息。

1 个答案:

答案 0 :(得分:1)

如果<Description>包含符合XML的HTML,您可以将其加载到另一个XDocument变量:

XDocument description = XDocument.Parse(feed.Element("description").Value);

然后,您可以使用另一个LINQ-to-XML查询来获取您感兴趣的<Description>值的任何部分。

否则,您需要另一种可以处理HTML的类型,例如HtmlAgilityPack&#39; HtmlDocument