简体   繁体   English

从XDocument中选择XElement

[英]Selecting a XElement from a XDocument

I really didn't want to ask for help as I know I'll eventually figure it out, but I've spent too much time, if the document had parent tags or a better structure, it would be a piece of cake. 我真的不想寻求帮助,因为我知道我最终会弄清楚,但我花了太多时间,如果文件有父标签或更好的结构,那将是一块蛋糕。 Sadly I'm downloading the document, and I just can't figure out how to get the data. 可悲的是我正在下载文档,我无法弄清楚如何获取数据。

I've tried aa few linq queries and a foreach using XElement as an iterator. 我尝试了一些使用XElement作为迭代器的linq查询和foreach。 Anyway here's an example of the structure. 无论如何这里是结构的一个例子。

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/SiteExplorerService/V1/InlinkDataResponse.xsd" totalResultsAvailable="247930100" firstResultPosition="99" totalResultsReturned="100">
 <Result>
  <Title>Adobe - Adobe Reader</Title> 
  <Url>http://get.adobe.com/fr/reader/</Url> 
  <ClickUrl>http://get.adobe.com/fr/reader/</ClickUrl> 
  </Result>
 <Result>
  <Title>Religious Tolerance</Title> 
  <Url>http://www.religioustolerance.org/</Url> 
  <ClickUrl>http://www.religioustolerance.org/</ClickUrl> 
  </Result>
 <Result>
  <Title>Applications Internet riches (RIA) | Adobe Flash Player</Title> 
  <Url>http://www.adobe.com/fr/products/flashplayer/</Url> 
  <ClickUrl>http://www.adobe.com/fr/products/flashplayer/</ClickUrl> 
  </Result>
 <Result>
  <Title>photo management software | Adobe Photoshop Lightroom 3</Title> 
  <Url>http://www.adobe.com/products/photoshoplightroom/</Url> 
  <ClickUrl>http://www.adobe.com/products/photoshoplightroom/</ClickUrl> 
  </Result>
 <Result>
  <Title>Battle for Wesnoth</Title> 
  <Url>http://www.wesnoth.org/</Url> 
  <ClickUrl>http://www.wesnoth.org/</ClickUrl> 
  </Result>
</ResultSet>

Here's an example of a latest snippet. 这是最新代码段的示例。

foreach (XElement ele in xDoc.Descendants("ResultSet").Elements("Result"))
                {
                    CollectedUris.Add(ele.Element("Url").Value);
                }

You'll need to add an XNamespace : 您需要添加XNamespace

XNamespace ns = "urn:yahoo:srch";

var query = xDoc.Root.Descendants( ns + "Result" ).Elements( ns + "Url" )

foreach( XElement e in query )
{
    CollectedUris.Add( e.Value );
}

Edit : 编辑
A LINQ solution for bonus points: LINQ奖励积分解决方案:

xDoc.Root.Descendants( ns + "Result" )
    .Elements( ns + "Url" )
    .Select( x => x.Value ).ToList()
    .ForEach( CollectedUris.Add );

I'm assuming you want all <Url> elements in the document. 我假设您想要文档中的所有 <Url>元素。 If that's the case, then your loop is almost there. 如果是这种情况,那么你的循环几乎就在那里。 You will want to do the following. 您需要执行以下操作。

using System.Xml.Linq;

foreach (XElement ele in xDoc.Root.Descendants("Result").Descendants("Url")
{
    CollectedUris.Add(ele.Value);
}

Root gets you a reference to the root element, and the following Descendants statement returns only the <Result> nodes. Root您提供对根元素的引用,以下Descendants语句仅返回<Result>节点。 The last Descendants statement further constrains the <Result> node enumerator to only return <Url> elements. 最后一个Descendants语句进一步限制<Result>节点枚举器仅返回<Url>元素。

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

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