I'm a beginner when it comes to XML. I created a simple XML file and tried to parse it and assign the values into variables. It worked but the method I used made me wonder if there're better ways, more elegant if you will, for this task. Are there any?
Here's my XML file:
<start>
<record>
<var1>hello</var1>
<var2>world</var2>
</record>
<record>
<var1>another</var1>
<var2>one</var2>
</record>
</start>
Here's the method I used:
string var1 = "", var2 = "";
using(XmlReader r = XmlReader.Create(file))
{
while(r.Read())
{
if (r.MoveToContent() == XmlNodeType.Element)
{
if(r.Name == "record")
{
var1 = "";
var2 = "";
}
else if(r.Name = "var1")
var1 = r.ReadElementString();
else if(r.Name = "var2")
var2 = r.ReadElementString();
}
else if(r.MoveToContent() == XmlNodeType.EndElement && r.Name == "record")
{
Console.WriteLine(var1 + " " + var2);
}
}
}