xml - Null Reference Exception in C# application -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
following code throws
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 line
age = int.parse(node.attributes["age"].value);
in locals window can see, reference attribut "age" remains null.
the .xml-file structured that:
<people> <person> <name>testperon</name> <age>29</age> <email>me@testmail.com</email> </person> </people>
thank you!
there no attribute name, element.
Comments
Post a Comment