I'm making an application which saves and loads data from an XML file.
Here is my xml:
<?xml version="1.0" encoding="utf-8" ?>
<storage>
<Save Name ="Lifeline">
<Seconds>12</Seconds>
<Minutes>24</Minutes>
<Hours>9</Hours>
<Days>25</Days>
<Months>8</Months>
<Years>2010</Years>
<Health>90</Health>
<Mood>100</Mood>
</Save>
<Save Name ="Hellcode">
<Seconds>24</Seconds>
<Minutes>48</Minutes>
<Hours>18</Hours>
<Days>15</Days>
<Months>4</Months>
<Years>1995</Years>
<Health>50</Health>
<Mood>50</Mood>
</Save>
</storage>
The thing is that I whant to specify the "save" by loading "name" from a listbox in such a way
System.IO.StreamReader sr = new System.IO.StreamReader(@"Saves.xml");
System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
System.Xml.XmlDocument save = new System.Xml.XmlDocument();
save.Load(xr);
string name = lstSave.SelectedItem.ToString();
XmlNodeList saveItems = save.SelectNodes("Storage/Save[@Name = name]");
XmlNode seconds = saveItems.Item(0).SelectSingleNode("Seconds");
sec = Int32.Parse(seconds.InnerText);
XmlNode minutes = saveItems.Item(0).SelectSingleNode("Minutes");
min = Int32.Parse(minutes.InnerText);
XmlNode hours = saveItems.Item(0).SelectSingleNode("Hours");
hour = Int32.Parse(hours.InnerText);
XmlNode days = saveItems.Item(0).SelectSingleNode("Days");
day = Int32.Parse(days.InnerText);
XmlNode months = saveItems.Item(0).SelectSingleNode("Months");
month = Int32.Parse(months.InnerText);
XmlNode years = saveItems.Item(0).SelectSingleNode("Years");
year = Int32.Parse(years.InnerText);
XmlNode health_ = saveItems.Item(0).SelectSingleNode("Health");
health = Int32.Parse(health_.InnerText);
XmlNode mood_ = saveItems.Item(0).SelectSingleNode("Mood");
mood = Int32.Parse(mood_.InnerText);
When I try to run the application the compiler gives NullReferenceException was unhandled "Object reference not set to an instance of an object" on
XmlNode seconds = saveItems.Item(0).SelectSingleNode("Seconds");
So my question is what's wrong and what should I do?
Edit: I've even tried this
foreach (XmlNode xn in saveItems)
{
sec = Int32.Parse(xn["Seconds"].InnerText);
min = Int32.Parse(xn["Minutes"].InnerText);
hour = Int32.Parse(xn["Hours"].InnerText);
day = Int32.Parse(xn["Days"].InnerText);
month = Int32.Parse(xn["Months"].InnerText);
year = Int32.Parse(xn["Years"].InnerText);
health = Int32.Parse(xn["Health"].InnerText);
mood = Int32.Parse(xn["Mood"].InnerText);
}
but nothing loads at all
===================================================================
just to get this quetion easier to understand. Here is the code which works and loads all needed data for application, BUT it loads only from "Lifeline" node. While compiling, there are no exception and all works pretty fine.
System.IO.StreamReader sr = new System.IO.StreamReader(@"Saves.xml");
System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
System.Xml.XmlDocument save = new System.Xml.XmlDocument();
save.Load(xr);
XmlNodeList saveItems = save.SelectNodes("Storage/Save");
XmlNode seconds = saveItems.Item(0).SelectSingleNode("Seconds");
sec = Int32.Parse(seconds.InnerText);
XmlNode minutes = saveItems.Item(0).SelectSingleNode("Minutes");
min = Int32.Parse(minutes.InnerText);
XmlNode hours = saveItems.Item(0).SelectSingleNode("Hours");
hour = Int32.Parse(hours.InnerText);
XmlNode days = saveItems.Item(0).SelectSingleNode("Days");
day = Int32.Parse(days.InnerText);
XmlNode months = saveItems.Item(0).SelectSingleNode("Months");
month = Int32.Parse(months.InnerText);
XmlNode years = saveItems.Item(0).SelectSingleNode("Years");
year = Int32.Parse(years.InnerText);
XmlNode health_ = saveItems.Item(0).SelectSingleNode("Health");
health = Int32.Parse(health_.InnerText);
XmlNode mood_ = saveItems.Item(0).SelectSingleNode("Mood");
mood = Int32.Parse(mood_.InnerText);
The problem is that I want to have an ability to choose nodes by "Name" attribute, and I don't know hot to do it using the listbox. Those "Lifeline" and "Hellcode" are like account names, and the user should choose which account data to load.