Erez
Erez

Reputation: 1953

Getting Child nodes of an xDocument element while itereating through the 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.

structure:

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

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

and then:

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

Thank you, Erez

Upvotes: 1

Views: 9864

Answers (2)

Chuck Savage
Chuck Savage

Reputation: 11955

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

Upvotes: 4

Gerhard Powell
Gerhard Powell

Reputation: 6175

Are you meaning select inside a select?

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

Upvotes: 1

Related Questions