I have an Xml file where I want to loop through each module node which increments by 1. Here is a sample of my xml file:
<PersonDetails>
<PersonTitle>Teacher</PersonTitle>
<Keystage3>
<Subject>
<subjectName>maths</subjectName>
<subjectId>qq1</subjectId>
<subjectvalue>20</subjectvalue>
<subjectscore />
</Subject>
<Subject>
<subjectName>science</subjectName>
<subjectId>sla1s</subjectId>
<subjectvalue>25</subjectvalue>
<subjectscore />
</Subject>
</Keystage3>
</PersonDetails>
I want to loop through the xml file and get all data of Subject for both
<Subject>
node and store each value inside a variable.
I have a piece of code which gets the value from a specific node and outputs it on a textfield.
here is the code I have so far:
public partial class Form1 : Form
{
private string subName, subId, subvalue;
public XmlDocument Doc;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader("data.xml");
XmlNodeType type;
while (reader.Read()) {
type = reader.NodeType;
if(type == XmlNodeType.Element)
{
if (reader.Name == "subjectName")
{
reader.Read();
textBox1.Text = reader.Value;
}
if (reader.Name == "subjectId")
{
reader.Read();
textBox2.Text = reader.Value;
}
if (reader.Name == "subjectvalue")
{
reader.Read();
textBox3.Text = reader.Value;
}
}
}
reader.Close();
}
How can I make it so that the output recieved is something like:
{maths,qq1,20}
{science,sla1s,25}
Using some LINQ-Magic you can do the folowing:
This will print:
Includes: