Parul Maheshwari

Parul Maheshwari

  • NA
  • 13
  • 6.3k

Read all xml nodes with same name and their childNodes in c#

Aug 17 2017 3:20 PM
My xml file looks something like this:
<academic>
<school1>
<grade1>
<student>
<name>John</name>
<age>7</age>
</student>
<student>
<name>Julie</name>
<age>8</age>
</student>
</grade1>
<grade2>
<student>
<name>Michael</name>
<age>8</age>
</student>
<student>
<name>Dave</name>
<age>8</age>
</student>
</grade2>
</school1>
<school2>
<grade1>
<student>
<name>Mary</name>
<age>9</age>
</student>
<student>
<name>Johnson</name>
<age>7</age>
</student>
</grade1>
</school2>
</academic>

The tags 'school1', 'school2', 'grade1', 'grade2' are dynamically created. And number of <student> tags under each grade are also dynamic.

I want to read all <student> and its childNodes for <grade1>, <grade2> separately. How can I do that?

Since there are dynamic tags, I am able to read all <student> together and store it in a List<string>.
But I want to read them individually according to the grade.

Answers (4)