LINQ请求以获取具有属性的最远的xml子节点 [英] LINQ request for getting the furthest xml child node with an attribute
问题描述
我有一个定义项目/位置类别的xml文件,其ID如下:
I have an xml file that defines item/location categories, and their id's like follow:
<?xml version="1.0" encoding="UTF-8"?>
<Eve_App>
<Item_Types>
<Ore>
<Arkonor>
<Arkonor ID="22"/>
<Compressed_Arkonor ID="28367"/>
<Compressed_Crimson_Arkonor ID="28385"/>
<Compressed_Prime_Arkonor ID="28387"/>
<Crimson_Arkonor ID="17425"/>
<Prime_Arkonor ID="17426"/>
</Arkonor>
<Bistot>
<Bistot ID="1223"/>
<Compressed_Bistot ID="28388"/>
<Compressed_Monoclinic_Bistot ID="28389"/>
<Compressed_Triclinic_Bistot ID="28390"/>
<Monoclinic_Bistot ID="17429"/>
<Triclinic_Bistot ID="17428"/>
</Bistot>
<Crokite>
<Compressed_Crokite ID="28391"/>
<Compressed_Crystalline_Crokite ID="28392"/>
<Compressed_Sharp_Crokite ID="28393"/>
<Crokite ID="1225"/>
<Crystalline_Crokite ID="17433"/>
<Sharp_Crokite ID="17432"/>
</Crokite>
...
</Ore>
<Ice>
<Blue_Ice ID="16264"/>
<CLear_Icicle ID="16262"/>
<Compressed_Blue_Ice ID="28433"/>
...
</Ice>
<Gas>
<Booster_Gas_Clouds>
<Amber_Cytoserocin ID="25268"/>
<Amber_Mykoserocin ID="28694"/>
<Azure_Cytoserocin ID="25279"/>
<Azure_Mykoserocin ID="28695"/>
<Celadon_Cytoserocin ID="25275"/>
<Celadon_Mykoserocin ID="28696"/>
...
</Booster_Gas_Clouds>
<Fullerenes>
<Fullerite_C28 ID="30375"/>
<Fullerite_C32 ID="30376"/>
...
</Fullerenes>
</Gas>
<Mineral>
<Isogen ID="37"/>
<Megacyte ID="40"/>
<Mexallon ID="36"/>
<Morphite ID=""/>
<Nocxium ID="38"/>
<Pyerite ID="35"/>
<Tritanium ID="34"/>
<Zydrine ID="39"/>
</Mineral>
...
如您所见,包含ID
的项目不一定处于同一级别.我想知道是否存在LINQ请求,该请求会使我获得与ID
关联的节点的名称,而与Item_Types
节点中该项目的级别无关.
As you can see, the items containing the ID
are not necessarily on the same level. I would like to know if there is a LINQ request that would get me the name of the node associated with the ID
that I would want independently of the level of that item in the Item_Types
node.
以下请求是否可以在所有嵌套的子节点内进行迭代?
Would the following request work in that it would iterate inside all of the nested children nodes?
IEnumerable<string> ids =
from el in xelement.Elements("Item_Types")
where (int)el.Attribute("ID") == "22" // For example
select (string)el.value;
目前我有这个:
IEnumerable<String> names = _xmlFile.Descendants()
.Where(x => x.Attributes().Any(a => a.Name.LocalName == "ID") && uint.Parse(x.Attributes().First(a => a.Name.LocalName == "ID").Value) == id)
.Select(t => t.Name.LocalName);
if (names.Count() != 0) // Error here
{
return names.ElementAt(0);
}
else
{
return "";
}
不幸的是,调用Count()
函数时出现以下错误:
Unfortunately, I get the following error when calling the Count()
function:
Input string was not in a correct format.
当我也查看names
变量的值时,也可以在调试中看到此错误.尽管当我只是不使用Count()
而仅return names.ElementAt(0);
时(我能够正确看到与该ID
关联的名称),这不会发生,但是如果ID
不存在,当我尝试return names.ElementAt(0);
(因此为什么要进行此检查).
I can see this error in debug when I look at the value of the names
variable too. This does not happen though when I just return names.ElementAt(0);
without using Count()
(I am able to see the name associated to that ID
properly), but then if the ID
doesn't exist, I get another error when I try to return names.ElementAt(0);
(thus why I do that check).
为什么会出现提到的Parse()
错误?
Why is it that I get the mentioned Parse()
error?
推荐答案
尝试以下方法:
IEnumerable<string> ids = xelement.Descendants()
.Where(x => x.Attributes().Any(a => a.Name.LocalName == "ID") && x.Attributes().First(a => a.Name.LocalName == "ID").Value == "28390")
.Select(t => t.Name.LocalName);
首先,我检查后代节点ID属性是否存在,如果存在,请检查其ID值.
First I check in descendant nodes that ID attribute exists and if it is I check it's value for ID needed.
这篇关于LINQ请求以获取具有属性的最远的xml子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!