繁体   English   中英

LINQ to XML-选择多个属性过滤器?

[英]LINQ to XML - Selecting with multiple attribute filters?

我是XmlTextReader的长期用户,但在注意到这里每个相关问题的答案是“使用LINQ”之后,我决定尝试一下,直到现在为止一切都很好。 我已经为此努力了一段时间,希望有人能提供帮助。

我的文档中包含以下部分;

  <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>

由于文档的结构,我需要“ action_areas”作为传递给初始选择的内容,即;

 var stuff = from item in xDoc.Descendants("action_areas")

所以,问题是什么语句我请遵照这样我就可以过滤器的基础上time_slice name属性和action_area id属性和aa_team id属性,所以我可以最终得到在包含在内容aa_team元素(9,7,5和上面例子中的2)和'select new'语句?

提前致谢。

尝试这个

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM