从 XDocument 中选择 XElement
我真的不想寻求帮助,因为我知道我最终会弄清楚,但我花了太多时间,如果文档有父标签或更好的结构,那将是小菜一碟。遗憾的是我正在下载该文档,但我不知道如何获取数据。
我尝试了一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加一个
XNamespace
:编辑:
奖励积分的 LINQ 解决方案:
You'll need to add an
XNamespace
:Edit:
A LINQ solution for bonus points:
我假设您想要文档中的所有
元素。如果是这样的话,那么你的循环就差不多完成了。您将需要执行以下操作。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.Root
gets you a reference to the root element, and the followingDescendants
statement returns only the<Result>
nodes. The lastDescendants
statement further constrains the<Result>
node enumerator to only return<Url>
elements.