如何获得Linq的后代? [英] How can I get descendants of descendant in Linq?

查看:55
本文介绍了如何获得Linq的后代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:因此,我有一个XML文件需要解析.经过研究,我已经能够获得第一套没有问题的节点,而且我真的很喜欢我可以返回它们的方式.

So I have an XML file which I need to parse. After research, I have been able to get the first set of nodes with out issue, and I really like the way I can return them.

我已经能够获得<Project>的直接子代.但是获得下一个后代(<tree>& <branch)并没有取得多少成果,我最终决定亲自问问Google和R + D ...如果可能的话,我想参加类似的课程样式输出,如下所示,即使代码已重组.

I have been able to get the immediate Child of <Project>. But getting the next descendants (<tree> & <branch), has not been quite so fruitful, and I finally decided to ask after google and R+D myself... If possible I'd like to have the similar class style output as shown below, even if the code is restructured.

XML结构

<Projects>
 <Project AccessTag="">
  <Title></Title>
  <ReaderURL></ReaderURL>
  <Status></Status>
  <tree>
   <branch></branch>
  </tree>
 </Project>
</Projects>

代码

XDocument xml = XDocument.Load(this.XMLFile);
var project = (
  from p in xml.Descendants("Project")
  where p.Attribute("AccessTag").Value == Trigger
  select new {
    title = p.Element("Title").Value,
    reader = p.Element("ReaderURL").Value,
    status = p.Element("Status").Value
    }).Single();
// output is project.title, project.reader, etc

经过所有研究,并根据以下答案进行了改进,现在完成的代码如下,并返回与class相似的var结果.在进一步研究该项目之后.我最终得到许多<tree><branch>元素作为父<Project>元素的子节点.每个树元素都具有唯一的number属性.

After all the research I did, and variations based off below answers, my finished code is now as follows, and returns a var result similar to that of a class. After working on the project further. I ended up with many <tree> and <branch> elements as child nodes of the parent <Project> element. Which each tree element has a unique number attribute.

XDocument xml = XDocument.Load(this.XMLFile);

var resulted = xml.Descendants("Project")
  .Where(a => (string)a.Attribute("AccessTag") == Trigger)
  .Select(a => new {
    Title = (string)a.Element("Title"),
    ReaderURL = (string)a.Element("ReaderURL"),
    Status = (string)a.Element("Status"),

    Tree = a.Elements("tree")
      .Where(b => (string)b.Attribute("number") == Num)
      .Select(b => new {...}).Single()
  }).FirstOrDefault();

就像下面提到的那样,我花了几分钟的时间进行奇迹和调试,然后才想起在第二个.Select()子句中使用.Single().因为我只想返回一个结果(并且我也不熟悉通过Enumerators进行操作).感谢您的回复,并为大家提供帮助!

Like mentioned below, I spent a couple minutes in wonder and debugging before I remembered to use .Single() on the 2nd .Select() clause. As I only wanted one result returned ( and I also am not familiar with going through Enumerators yet ). Thanks for the responses, and help everyone!

推荐答案

尝试一下:-

var result = xml.Descendants("Project")
           .Where(x => (string)x.Attribute("AccessTag") == "Trigger")
           .Select(x => new
  {
     Title = (string)x.Element("Title"),
     ReaderURL = (string)x.Element("ReaderURL"),
     Status = (string)x.Element("Status"),
     Branch = x.Descendants("tree") //Here are fetching the Descendants
              .Select(z => (string)z.Element("branch")).FirstOrDefault()
   }).FirstOrDefault();

请注意,我在获取Branch时正在使用FirstOrDefault,因为我认为您只需要第一个Branch元素,如果不是这种情况,则将FirstOrDefault替换为ToList(),它将返回所有branch元素.

Please note I am using FirstOrDefault while fetching Branch as I am considering you need just the first Branch element, if that is not the case, replace FirstOrDefault with ToList() and it will return all the branch elements.

这篇关于如何获得Linq的后代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆