C# LINQ到XML-使用多属性过滤器进行选择?
我是XmlTextReader的长期用户,但注意到这里每个相关问题的答案都是'use LINQ'之后,我决定冒险一试,直到现在一切都进展顺利。这件事我已经讨论了一段时间了,希望有人能帮上忙 我的文件中有以下部分:C# LINQ到XML-使用多属性过滤器进行选择?,c#,xml,linq,C#,Xml,Linq,我是XmlTextReader的长期用户,但注意到这里每个相关问题的答案都是'use LINQ'之后,我决定冒险一试,直到现在一切都进展顺利。这件事我已经讨论了一段时间了,希望有人能帮上忙 我的文件中有以下部分: <action_areas> <time_slice name="0 - 5" id="1"> <action_area id="1"> <aa_team id="1000">9</aa_te
<action_areas>
<time_slice name="0 - 5" id="1">
<action_area id="1">
<aa_team id="1000">9</aa_team>
<aa_team id="1001">7</aa_team>
</action_area>
<action_area id="2">
<aa_team id="1000">5</aa_team>
<aa_team id="1001">2</aa_team>
因此,问题是我应该遵循哪些语句,以便根据time\u slice
name属性和action\u area
id属性和aa\u team
id属性进行过滤,从而最终获得aa\u team
元素中包含的内容(在上述实例中为9、7、5和2)使用“选择新的”
语句
提前谢谢。试试这个
string timeSliceToSearchFor = "0 - 5";
string actionAreaToSearchFor = "1";
string aaTeamIdToSearchFor = "1001";
// Returns 7
string value = xDoc .Descendants("action_areas")
.Elements("time_slice")
.Where(element => element.Attribute("name").Value == timeSliceToSearchFor)
.Elements("action_area")
.Where(element => element.Attribute("id").Value == actionAreaToSearchFor)
.Elements("aa_team")
.Where(element => element.Attribute("id").Value == aaTeamIdToSearchFor)
.Single().Value;
string timeSliceToSearchFor = "0 - 5";
string actionAreaToSearchFor = "1";
string aaTeamIdToSearchFor = "1001";
// Returns 7
string value = xDoc .Descendants("action_areas")
.Elements("time_slice")
.Where(element => element.Attribute("name").Value == timeSliceToSearchFor)
.Elements("action_area")
.Where(element => element.Attribute("id").Value == actionAreaToSearchFor)
.Elements("aa_team")
.Where(element => element.Attribute("id").Value == aaTeamIdToSearchFor)
.Single().Value;