获取一个XDocument元素的子节点,同时通过的XDocument itereating [英] Getting Child nodes of an xDocument element while itereating through the Xdocument

查看:120
本文介绍了获取一个XDocument元素的子节点,同时通过的XDocument itereating的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我的问题是不清楚。我有一大堆的元素,并从他们的XDocument中的每一个属性。现在,我想在每一次迭代让每一个节点的更多死者,并遍历它们,并得到所有的属性。

Sorry if my question was unclear. I got a bunch of elements and the attributes for each one of them from the xDocument. Now i want in every iteration to get more decedents of every node and iterate through them and get all of their attributes.

结构:

<Jobs>
     <Job attr1=val1 attr2=val2 attr3=val3>
          <InnerNode1 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode1>
          <InnerNode2 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode2>
          <InnerNode3 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode3>
     </job>
     <Job attr1=val4 attr2=val5>
          <InnerNode1 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode1>
          <InnerNode2 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode2>
          <InnerNode3 InnerAttr1=val6 InnerAttr2=7>
                     <InnerNodeChild1>
                        .........
                     </InnerNodeChild1>
                     <InnerNodeChild2>
                        ............
                     </InnerNodeChild2>
                     <InnerNodeChild3>
                        .......
                     </InnerNodeChild3>
          </InnerNode3>
     </Job>
     .....
     .....
     .....
     <OtherNodeInSameLevelAsJob>
     </OtherNodeInSameLevelAsJob>
</Jobs>

确定,对于每一个工作节点会出现只有一个 InnerNode1 并具有属性和它自身的内部节点。如果我想获得的所有属性,并从每一个InnerNode1的InnerNodeChilds但在运行直通的作业,就像在下面的例子中,我需要做什么?

OK, For every Job Node there will be just one InnerNode1 and it has attributes and inner nodes of it self. If I want to get all the attributes and the InnerNodeChilds from every InnerNode1 but while running thru the jobs, like in the next example, What do i need to do?

  XDocument xDoc = XDocument.Load(xDr);
            var Jobs = from Job in xDoc.Descendants("Job")
                       select new {  
                            JobID = Job.Attribute("JobID").Value,
                            JobName = Job.Attribute("JobName").Value,
                            ........
                            ........
                            ........
                        };

和则:

 foreach(var Job in Jobs){
        string JobId = Job.JobID;
        string JobName = job.JobName;
        .........
        .........
        .........
 }

感谢您,
埃雷兹

Thank you, Erez

推荐答案

获取每 InnerNode1 很简单,你只需要调用 .Descendants( InnerNode1),你就会有他们每个人的列表。下面是可能会为你工作的例子。我叫父节点上获取其作业名称和ID。

Getting every InnerNode1 is very simple, you just call .Descendants("InnerNode1") and you'll have a list of every one of them. Here's an example that might work for you. I call parent on the node to get its job name and id.

var innerchilds = xDoc.Descendants("InnerNode1").Select(x => new {
    JobID = x.Parent.Attribute("JobID").Value,
    JobName = x.Parent.Attribute("JobName").Value,
    ...
    });

这篇关于获取一个XDocument元素的子节点,同时通过的XDocument itereating的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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