I got a problem with the XMLReader in C#.
i got this 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);
}
}
}
this is my XML-File:
<?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>
The ListItems are added to the list but the reader.Value is always empty.
How can I get this to work?
many thanks
Tobi
EDIT:
now I got the following code:
private void countryXMLReader () { List twentyFour = new List();
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());
}
but it don't really got the values.
what can be my problem?
EDIT2:
this is the code I use:
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());
}
but the List.Count remains at 0. and I call this Method like this:
public Page1()
{
InitializeComponent();
countryXMLReader();
}
I also tested it with a button, but same result