获取具有属性的最远 xml 子节点的 LINQ 请求

LINQ request for getting the furthest xml child node with an attribute

我有一个定义 item/location 类别的 xml 文件,它们的 ID 如下所示:

<?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 节点中该项目的级别。

编辑:

以下请求是否可以在所有嵌套子节点内迭代?

IEnumerable<string> ids =
       from el in xelement.Elements("Item_Types")
       where (int)el.Attribute("ID") == "22" // For example
       select (string)el.value;

编辑 2:

目前我有这个:

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() 函数时出现以下错误:

Input string was not in a correct format.

当我查看 names 变量的值时,我也可以在调试中看到此错误。虽然当我只是 return names.ElementAt(0); 而不使用 Count() 时(我能够正确地看到与那个 ID 关联的名称),但如果 ID 没有t 存在,当我尝试 return names.ElementAt(0); 时出现另一个错误(这就是我进行检查的原因)。

为什么我会收到提到的 Parse() 错误?

试试这个:

    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 的值。

问题中没有很清楚地说明(我认为预期输出的示例可以阐明这一点),但假设您想要获取 ID 属性等于特定值的元素名称和目标元素可以在 XML 文档中的任何级别深度,您可以使用 Descendants() 方法,如下所示:

IEnumerable<string> names =
       from el in xelement.Descendants()
       where (int)el.Attribute("ID") == "22"
       select (string)el.Name.LocalName;

(int)el.Attribute("ID") 只是 return 0 - 而不是抛出异常 - 如果当前 el 中没有 ID 属性,那么你可以跳过检查 ID 属性是否存在。