Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with reading from an XML with attributes in C#
    primarykey
    data
    text
    <p>I'm making an application which saves and loads data from an XML file.</p> <p>Here is my xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;storage&gt; &lt;Save Name ="Lifeline"&gt; &lt;Seconds&gt;12&lt;/Seconds&gt; &lt;Minutes&gt;24&lt;/Minutes&gt; &lt;Hours&gt;9&lt;/Hours&gt; &lt;Days&gt;25&lt;/Days&gt; &lt;Months&gt;8&lt;/Months&gt; &lt;Years&gt;2010&lt;/Years&gt; &lt;Health&gt;90&lt;/Health&gt; &lt;Mood&gt;100&lt;/Mood&gt; &lt;/Save&gt; &lt;Save Name ="Hellcode"&gt; &lt;Seconds&gt;24&lt;/Seconds&gt; &lt;Minutes&gt;48&lt;/Minutes&gt; &lt;Hours&gt;18&lt;/Hours&gt; &lt;Days&gt;15&lt;/Days&gt; &lt;Months&gt;4&lt;/Months&gt; &lt;Years&gt;1995&lt;/Years&gt; &lt;Health&gt;50&lt;/Health&gt; &lt;Mood&gt;50&lt;/Mood&gt; &lt;/Save&gt; &lt;/storage&gt; </code></pre> <p>The thing is that I whant to specify the "save" by loading "name" from a listbox in such a way</p> <pre><code> 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); </code></pre> <p>When I try to run the application the compiler gives NullReferenceException was unhandled "Object reference not set to an instance of an object" on </p> <pre><code>XmlNode seconds = saveItems.Item(0).SelectSingleNode("Seconds"); </code></pre> <p>So my question is what's wrong and what should I do?</p> <p>Edit: I've even tried this</p> <pre><code>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); } </code></pre> <p>but nothing loads at all</p> <p>===================================================================</p> <p>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.</p> <p>System.IO.StreamReader sr = new System.IO.StreamReader(@"Saves.xml");</p> <pre><code>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); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload