2

我有以下 XML 结构:

<Capabilities>
    <Capability ID="1" Name="Capability # 1">
        <Relations>
            <Relation RelatedTo="2" RelationType="Child"/>
            <Relation RelatedTo="3" RelationType="Child"/>
            <Relation RelatedTo="4" RelationType="Child"/>
            <Relation RelatedTo="5" RelationType="Child"/>
        </Relations>
    </Capability>
    <Capability ID="3" Name="Capability # 3">
        <Relations>
            <Relation RelatedTo="1" RelationType="Dependant" />
        </Relations>
    </Capability>
    <Capability ID="2" Name="Capability # 2">
        <Relations>
            <Relation RelatedTo="6" RelationType="Child" />
            <Relation RelatedTo="5" RelationType="Child" />
        </Relations>
    </Capability>
</Capabilities>

而且我需要查询所有具有 Child 关系且相关属性值为 5 的 Capabilities。到目前为止,我已经尝试使用以下查询,但我得到了一个“空引用异常”集合,我不知道如何做。

   var result = root.Elements("Capability")
                 .Where(c => c.Elements("Relations")
                            .Where(r => r.Attribute("RelatedTo").Value == "5"
                                && r.Attribute("").Value == Enum.GetName(typeof(RelationTypes), RelationTypes.Children)).Any());

关于我做错了什么或查询它的正确方法的任何线索?

4

2 回答 2

4
var capabilites = root.Descendants("Capability")
            .Where(c => c.Descendants("Relation")
                         .Any(r => (string)r.Attribute("RelatedTo") == "5"))
            .ToList();
于 2012-09-11T22:02:57.447 回答
1

这个:

var xDoc = XDocument.Parse(
@"<Capabilities>
<Capability ID=""1"" Name=""Capability # 1""> 
        <Relations> 
            <Relation RelatedTo=""2"" RelationType=""Child""/> 
            <Relation RelatedTo=""3"" RelationType=""Child""/> 
            <Relation RelatedTo=""4"" RelationType=""Child""/> 
            <Relation RelatedTo=""5"" RelationType=""Related""/> 
        </Relations> 
    </Capability> 
    <Capability ID=""3"" Name=""Capability # 3""> 
        <Relations> 
            <Relation RelatedTo=""1"" RelationType=""Dependant"" /> 
        </Relations> 
    </Capability> 
    <Capability ID=""2"" Name=""Capability # 2""> 
        <Relations> 
            <Relation RelatedTo=""6"" RelationType=""Child"" /> 
            <Relation RelatedTo=""5"" RelationType=""Child"" /> 
        </Relations> 
    </Capability> 
</Capabilities>");

var q = xDoc.Descendants("Capability").
            Where(c => c.Descendants("Relation").Where(r => (int)r.Attribute("RelatedTo") == 5).Any());

foreach (var r in q)
{
    Console.WriteLine(r);
}

结果是:

<Capability ID="1" Name="Capability # 1">
  <Relations>
    <Relation RelatedTo="2" RelationType="Child" />
    <Relation RelatedTo="3" RelationType="Child" />
    <Relation RelatedTo="4" RelationType="Child" />
    <Relation RelatedTo="5" RelationType="Related" />
  </Relations>
</Capability>
<Capability ID="2" Name="Capability # 2">
  <Relations>
    <Relation RelatedTo="6" RelationType="Child" />
    <Relation RelatedTo="5" RelationType="Child" />
  </Relations>
</Capability>
Press any key to continue . . .

我想你在追求什么...

于 2012-09-11T22:12:10.063 回答