如何使用LINQ获得通过名字元素XML [英] How to get elements by name in XML using LINQ
问题描述
我选择了这里的标题作为我的问题是我需要在这个例子中提到的项目节点。
我有以下的XML和我使用LINQ查询它有问题,我已经能够前解析XML - 但是我一直停留在这几个小时,希望有人可以提供帮助。
这是我下面的XML数据(例如数据):
< A:进入的xmlns:A =HTTP ://www.w3.org/2005/Atom>
< A:ID> 98765< / A:ID>
<信息>数据分类和LT; /信息>
<数据>
<项目>
<项目>
<&ID GT; 123456< / ID>
< VALUE>项目的One< /值>
< /项目>
<项目>
<&ID GT; 654321< / ID>
< VALUE>项目二< /值>
< /项目>
<项目>
< /数据>
<项目>
<项目>
<&ID GT; 123456< / ID>
< VALUE>项目的One< /值>
< /项目>
<项目>
<&ID GT; 654321< / ID>
< VALUE>项目二< /值>
< /项目>
<项目>
< A:作家>
< A:名称>目录及LT; / A:名称>
< / A:作家>
< / A:进入>
我希望能够提取根据项目的项目XML标签的ID,但是有一个项目标签下的数据我不希望这些节点在所有项目的条目 - 我想根/项目/ D / I,如果这表示为路径。我尝试了一切,我知道使用LINQ所以,如果有人可以帮助,事情需要注意,虽然这是它是基于系统的样本数据 - 格式不能改变,这样是不是一个可以接受的解决方案,结果
我似乎无法决定我要去哪里错了 - 每一个LINQ表达我尝试什么回报,我认为空间是一个问题,并试图整合这一点,但我兜兜结果
。解决方案必须在Silverlight和C#
我曾尝试工作如下:
的IEnumerable<&的XElement GT;节点=
element.Elements(),其中(E => e.Name.LocalName ==项目)。
但是,这让我所有的项目,包括在数据我不希望那些人。
如果我做我的XML下面我确实看到显示的元素的名称:
的XElement元素= XElement.Parse(数据);
的foreach(在element.Elements的XElement节点())
{
MessageBox.Show(node.Name.LocalName);
}
但是当我这样做,我不能见项目节点名称在所有 - 我检查了的XElement和它确实有节点,当我输出它上面的项目显示了信息和ID一起!
<名字code>的foreach(在element.Elements的XElement节点(项目))
{
MessageBox.Show(node.Name.LocalName);
}
假设元素
是< A:进入>
元素:
VAR IDS = element.Element(项目)
.Elements(项目)
。选择(项目=方式> item.Element(ID)值) ;
的
元素
和元素
方法返回唯一的直接孩子,不是所有的后裔,所以它不会返回<项目>
元素,它在<数据>
I've chosen the title here as my problem is I need to get the Item nodes mentioned in the example. I have the following XML and am having problems using LINQ to query it, I've been able to parse XML before - however I've been stuck on this for hours and hope someone can help. Here is my XML data below (example data):
<a:entry xmlns:a="http://www.w3.org/2005/Atom">
<a:id>98765</a:id>
<info>Data Catalogue</info>
<data>
<items>
<item>
<id>123456</id>
<value>Item One</value>
</item>
<item>
<id>654321</id>
<value>Item Two</value>
</item>
<items>
</data>
<items>
<item>
<id>123456</id>
<value>Item One</value>
</item>
<item>
<id>654321</id>
<value>Item Two</value>
</item>
<items>
<a:author>
<a:name>Catalogue</a:name>
</a:author>
</a:entry>
I want to be able to extract the ID from the Item XML tag under Items, however there is an Items Tag with Item entries under data I DO NOT want these nodes at all - I want root/items/id/id if this were expressed as path. I've tried everything I know with LINQ so if someone could help, things to note although this is sample data it is based on the system - the format cannot be changed so that is not an acceptable solution.
I can't seem to determine where I'm going wrong - every LINQ expression I try returns nothing, I think the namespace is an issue and have tried to integrate this but I'm going in circles.
Solution must work in Silverlight and C#
I have tried the following:
IEnumerable<XElement> nodes =
element.Elements().Where(e => e.Name.LocalName == "items")
However this gets me all the "items" including the ones under "data" I don't want those.
If I do the following on my XML I do see the Names of the Elements displayed:
XElement element = XElement.Parse(data);
foreach (XElement node in element.Elements())
{
MessageBox.Show(node.Name.LocalName);
}
However when I do this I cannot see the node names under items at all - I've checked the XElement and it does have the node and when I output the names above it "items" shows up along with info and id!
foreach (XElement node in element.Elements("items"))
{
MessageBox.Show(node.Name.LocalName);
}
Assuming element
is your <a:entry>
element:
var ids = element.Element("items")
.Elements("item")
.Select(item => item.Element("id").Value);
The Element
and Elements
methods return only direct children, not all descendants, so it doesn't return the <items>
element which is under <data>
这篇关于如何使用LINQ获得通过名字元素XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!