Following code throws a
System.NullReferenceException
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "XML-Datei auswählen";
ofd.Filter = "XML-Dateien|*.xml";
ofd.InitialDirectory = @"C:\";
if (ofd.ShowDialog() == DialogResult.OK)
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(ofd.FileName);
foreach (XmlNode node in xDoc.SelectNodes("People/Person"))
{
int age = int.Parse(node.Attributes["Age"].Value);
MessageBox.Show((age + 1).ToString());
}
}
}
The error occurs in the line
age = int.Parse(node.Attributes["Age"].Value);
In the locals window I can see, that the reference for the attribut "Age" remains null.
The .xml-file is structured like that:
<People>
<Person>
<Name>TestPeron</Name>
<Age>29</Age>
<Email>me@testmail.com</Email>
</Person>
</People>
Thank you!