Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select XML node by attribute and use it's child nodes data?
    primarykey
    data
    text
    <p>Here is my XML file</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; </code></pre> <p> </p> <p>Here is a code which get's data from XML and loads it into application.</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); 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 this code loads data inly from "Lifeline" node. I would like to use a listbox and be able to choose from which node to load data. </p> <p>I've tried to take string from listbox item content and then use such a line </p> <pre><code>XmlNodeList saveItems = save.SelectNodes(string.Format("storage/Save[@Name = '{0}']", name)); </code></pre> <p>variable "name" is a string from listboxe's item. While compiled this code gives exception. Do somebody knows a way how to select by attribute and load nedeed data from that XML?</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.
 

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