I have the following XML String (XMLText) :
<?xml version="1.0" encoding="utf-8" ?>
<RateSheet>
<rate category="children" date="2016-04-21">
<value>1.375</value>
</rate>
<rate category="music" date="2016-05-01">
<value>2.475</value>
</rate>
</RateSheet>
I need to read the following information from that XML: category, date und value
I can read category and date - but I can not read the data for value
I have this code:
using (XmlReader reader= XmlReader.Create(new StringReader(XMLText)))
{
while (reader.ReadToFollowing("rate"))
{
Rate rate = new Rate();
reader.MoveToFirstAttribute();
rate.Category = reader.Value; //text of current Node : Catagory
//*************************************************************************************
reader.MoveToNextAttribute(); //text of current Node : Date
DateTime myDate;
if ( DateTime.TryParse( reader.Value, out myDate) )
{
rate.Date = myDate;
}
//*************************************************************************************
reader.ReadToFollowing("value"); //should be 1.375 or 2.475 - but is always empty ("")
Console.WriteLine("value Element=" + reader.Value); //test: reader.Value does not the data
decimal myValue;
if (Decimal.TryParse(reader.Value, out myValue))
{
rate.Value = myValue;
}
else
{
rate.Value = -1; // this is what happens because reader.value == ""
}
//return collection with result
myRates.Add(rate);
}
}