I'm trying to read a XElement and convert it to a decimal. Sumkwh is decimal and the input is:
<detaillinie>
<periode>2016-04-27 06:30:00</periode>
<kWh>0,35225</kWh>
</detaillinie>
<detaillinie>
<periode>2016-04-27 06:45:00</periode>
<kWh>0,35225</kWh>
</detaillinie>
<detaillinie>
<periode>2016-04-27 07:00:00</periode>
<kWh>0,17875</kWh>
</detaillinie>
The code looks like this:
var query =
from timeserie in xdoc.Descendants("detaillinie").AsEnumerable()
select new TimeSerie
{
Sumdato = (DateTime)timeserie.Element("periode"),
Sumkwh = (decimal)timeserie.Element("kWh")
}
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Inputstring was in a wrong format.
If I manually type
Sumkwh = decimal.parse("0,2435")
I get no error.
I have tryed with
Sumkwh = decimal.parse(timeserie.Element("kWh"))
it gives the error "Cannot convert from 'system.xml.linq.xelement' to string.
Any help please :)
timeserie.Element("kWh")
is a XElement
, you need to grab it's Value
.
Use
decimal.Parse(timeserie.Element("kWh").Value)