1

抱歉,如果我的问题不清楚。我从 xDocument 中获得了一堆元素和每个元素的属性。现在我希望在每次迭代中获得每个节点的更多死者并遍历它们并获取它们的所有属性。

结构体:

<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,但是在通过作业运行时,就像在下一个示例中一样,我需要做什么?

  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;
        .........
        .........
        .........
 }

谢谢你,埃雷兹

4

2 回答 2

4

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,
    ...
    });
于 2012-06-14T15:52:30.400 回答
1

你的意思是选择里面的选择吗?

var Jobs = from Job in xDoc.Descendants("Job")
                   select new {  
                        JobID = Job.Attribute("JobID").Value,
                        JobName = Job.Attribute("JobName").Value,
                        InnerNode = from inner in Job.Elements("InnerNode")
                            select new
                            {
                               Name = inner.Attribute("Name")
                            }
                        ........
                        ........
                        ........
                    };
于 2012-06-14T15:29:44.453 回答