从 XDocument 中选择 XElement

发布于 2024-09-14 02:35:47 字数 1885 浏览 2 评论 0原文

我真的不想寻求帮助,因为我知道我最终会弄清楚,但我花了太多时间,如果文档有父标签或更好的结构,那将是小菜一碟。遗憾的是我正在下载该文档,但我不知道如何获取数据。

我尝试了一些 linq 查询和使用 XElement 作为迭代器的 foreach。无论如何,这是一个结构示例。

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

这是最新片段的示例。

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

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 a a few linq queries and a foreach using XElement as an iterator. 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);
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅暮の光 2024-09-21 02:35:47

您需要添加一个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 );
}

编辑
奖励积分的 LINQ 解决方案:

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

You'll need to add an 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:

xDoc.Root.Descendants( ns + "Result" )
    .Elements( ns + "Url" )
    .Select( x => x.Value ).ToList()
    .ForEach( CollectedUris.Add );
绾颜 2024-09-21 02:35:47

我假设您想要文档中的所有 元素。如果是这样的话,那么你的循环就差不多完成了。您将需要执行以下操作。

using System.Xml.Linq;

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

Root 获取对根元素的引用,下面的 Descendants 语句仅返回 节点。最后一个Descendants语句进一步将节点枚举器限制为仅返回元素。

I'm assuming you want all <Url> elements in the document. 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. The last Descendants statement further constrains the <Result> node enumerator to only return <Url> elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文