Save XMLData to List with XMLReader

dciso

New Member
I got a problem with the XMLReader in C#. i got this Code:\[code\]private void countryXMLReader () { XmlTextReader reader = new XmlTextReader("expenses.xml"); List<string> twentyFour = new List<string>(); while (reader.Read()) { if (reader.Name.Equals("_24h")) { twentyFour.Add(reader.Value); } if (reader.Name == "_14h") { //MessageBox.Show(reader.Name); } } }\[/code\]this is my XML-File:\[code\]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><expenses> <country> <name>Germany</name> <ID>D</ID> <_24h>42</_24h> <_14h>28</_14h> <_8h>14</_8h> <overnight>100</overnight> </country> <country> <name>India</name> <ID>IND</ID> <_24h>30</_24h> <_14h>20</_14h> <_8h>10</_8h> <overnight>120</overnight> </country></expenses>\[/code\]The ListItems are added to the list but the reader.Value is always empty.How can I get this to work?many thanksTobiEDIT:now I got the following code:private void countryXMLReader () { List twentyFour = new List();\[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 => i.Value) .ToList()); }\[/code\]but it don't really got the values.what can be my problem?EDIT2:this is the code I use:\[code\]private void countryXMLReader () { List<string> twentyFour = new List<string>(); 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 => i.Value) .ToList()); }\[/code\]but the List.Count remains at 0. and I call this Method like this:\[code\]public Page1() { InitializeComponent(); countryXMLReader(); }\[/code\]I also tested it with a button, but same result
 
Back
Top