【问题标题】:Linq to XML querying an XDocLinq to XML 查询 XDoc
【发布时间】:2014-04-25 15:23:01
【问题描述】:

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

这是我的 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

                        };

按预期返回“描述”:

<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>

那么,我如何具体访问“span class= psrch-Description”之间的信息,同时仍然能够访问链接和标题等?

**因为我不是在寻找类似的东西

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

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

【问题讨论】:

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


    【解决方案1】:

    如果&lt;Description&gt; 包含符合XML 的HTML,您可以将其加载到另一个XDocument 变量中:

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

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

    否则,您将需要另一种可以处理 HTML 的类型,例如 HtmlAgilityPackHtmlDocument

    【讨论】:

    • 我还能将描述添加到同一个 RSS 模型吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多