简体   繁体   English

LINQ to XML新手问题:返回多个结果

[英]LINQ to XML Newbie Question: Returning More Than One Result

Greetings! 问候!

I'm working on wrapping my head around LINQ. 我正在努力绕着LINQ缠头。 If I had some XML such as this loaded into an XDocument object: 如果我有一些这样的XML加载到XDocument对象中:

<Root>
    <GroupA>
        <Item attrib1="aaa" attrib2="000" attrib3="true" />
    </GroupA>
    <GroupB>
        <Item attrib1="bbb" attrib2="111" attrib3="true" />
        <Item attrib1="ccc" attrib2="222" attrib3="false" />
        <Item attrib1="ddd" attrib2="333" attrib3="true" />
    </GroupB>
    <GroupC>
        <Item attrib1="eee" attrib2="444" attrib3="true" />
        <Item attrib1="fff" attrib2="555" attrib3="true" />
    </GroupC>
</Root>

I'd like to get the attribute values of all of the Item child elements of a Group element. 我想获取Group元素的所有Item子元素的属性值。 Here's what my query looks like: 这是我的查询的样子:

var results = from thegroup in l_theDoc.Elements("Root").Elements(groupName)
              select new
              { 
                 attrib1_val = thegroup.Element("Item").Attribute("attrib1").Value,      
                 attrib2_val = thegroup.Element("Item").Attribute("attrib2").Value,
              };

The query works, but if for example the groupName variable contains "GroupB", only one result (the first Item element) is returned instead of three. 该查询有效,但是如果例如groupName变量包含“GroupB”,则只返回一个结果(第一个Item元素)而不是三个。 Am I missing something? 我错过了什么吗?

XElement e = XElement.Parse(testStr);

string groupName = "GroupB";
var items = from g in e.Elements(groupName)
            from i in g.Elements("Item")
            select new {
                           attr1 = (string)i.Attribute("attrib1"),
                           attr2 = (string)i.Attribute("attrib2")
                       };

foreach (var item in items)
{
    Console.WriteLine(item.attr1 + ":" + item.attr2);
}

Yes, .Element() only returns the first matching element. 是的,.Element()只返回第一个匹配的元素。 You want .Elements() and you need to re-write your query somewhat: 你想要.Elements(),你需要在某种程度上重新编写你的查询:

var results = from group in l_theDoc.Root.Elements(groupName)
              select new
              {
                  items = from i in group.Elements("Item")
                          select new 
                          {
                              attrib1_val = i.Attribute("attrib1").Value,
                              attrib2_val = i.Attribute("attrib2").Value
                          }
              };

Here's the query method form of the answer: 这是答案的查询方法形式:

var items = 
  e.Elements("GroupB")
    .SelectMany(g => g.Elements("Item"))
    .Select(i => new {
      attr1 = i.Attribute("attrib1").Value,
      attr2 = i.Attribute("attrib2").Value,
      attr3 = i.Attribute("attrib3").Value
    } )
    .ToList()

Another possibility is using a where clause: 另一种可能是使用where子句:

var groupName = "GroupB";
var results = from theitem in doc.Descendants("Item")
              where theitem.Parent.Name == groupName
              select new 
              { 
                  attrib1_val = theitem.Attribute("attrib1").Value,
                  attrib2_val = theitem.Attribute("attrib2").Value, 
              };

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

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