Note that there are some explanatory texts on larger screens.

plurals
  1. POSave XMLData to List with XMLReader
    primarykey
    data
    text
    <p>I got a problem with the XMLReader in C#. </p> <p>i got this Code:</p> <pre><code>private void countryXMLReader () { XmlTextReader reader = new XmlTextReader("expenses.xml"); List&lt;string&gt; twentyFour = new List&lt;string&gt;(); while (reader.Read()) { if (reader.Name.Equals("_24h")) { twentyFour.Add(reader.Value); } if (reader.Name == "_14h") { //MessageBox.Show(reader.Name); } } } </code></pre> <p>this is my XML-File:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;expenses&gt; &lt;country&gt; &lt;name&gt;Germany&lt;/name&gt; &lt;ID&gt;D&lt;/ID&gt; &lt;_24h&gt;42&lt;/_24h&gt; &lt;_14h&gt;28&lt;/_14h&gt; &lt;_8h&gt;14&lt;/_8h&gt; &lt;overnight&gt;100&lt;/overnight&gt; &lt;/country&gt; &lt;country&gt; &lt;name&gt;India&lt;/name&gt; &lt;ID&gt;IND&lt;/ID&gt; &lt;_24h&gt;30&lt;/_24h&gt; &lt;_14h&gt;20&lt;/_14h&gt; &lt;_8h&gt;10&lt;/_8h&gt; &lt;overnight&gt;120&lt;/overnight&gt; &lt;/country&gt; &lt;/expenses&gt; </code></pre> <p>The ListItems are added to the list but the reader.Value is always empty.</p> <p>How can I get this to work?</p> <p>many thanks</p> <p>Tobi</p> <p>EDIT:</p> <p>now I got the following code:</p> <p>private void countryXMLReader () { List twentyFour = new List();</p> <pre><code> XDocument doc = XDocument.Load(@"C:\Users\Bl!tz\Documents\Visual Studio 2010\Projects\MBG.SimpleWizard\Demo\bin\Debug\expenses.xml"); twentyFour.AddRange(doc .Elements("expenses") .Descendants("country") .Descendants("_24h") .Select(i =&gt; i.Value) .ToList()); } </code></pre> <p>but it don't really got the values.</p> <p>what can be my problem?</p> <p>EDIT2:</p> <p>this is the code I use:</p> <pre><code>private void countryXMLReader () { List&lt;string&gt; twentyFour = new List&lt;string&gt;(); XDocument doc = XDocument.Load(@"expenses.xml"); twentyFour.AddRange(doc .Elements("expenses") .Descendants("country") .Descendants("name") .Descendants("ID") .Descendants("_24h") .Descendants("_14h") .Descendants("_8h") .Descendants("overnight") .Select(i =&gt; i.Value) .ToList()); } </code></pre> <p>but the List.Count remains at 0. and I call this Method like this:</p> <pre><code>public Page1() { InitializeComponent(); countryXMLReader(); } </code></pre> <p>I also tested it with a button, but same result</p>
    singulars
    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