0

This is the xml-File:

<?xml version='1.0' encoding='utf-8'?>
<ssbpreise>
    <ggvs>25.15</ggvs>
    <einblaspauschale>40</einblaspauschale>
    <pelletspreis>2.9</pelletspreis>
    <heizoel>0.4865</heizoel>
    <mwstpellets>5</mwstpellets>
    <mwstheizoel>22</mwstheizoel>
    <testparameter>testtest</testparameter>
</ssbpreise>

In my Code I try to convert the string

double einzelpreispellets = 0,
                einzelpreisheizoel = 0,
                einblaspauschale = 0,
                ggvs = 0,
                mwstpellets = 0,
                mwstheizoel = 0;
        try
        {
            if (devmode) writetolog(@"XML lesen.");

            using (XmlReader reader = XmlReader.Create(localFileserverBaseUrl + @"parameter.xml"))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        //return only when you have START tag  
                        switch (reader.Name.ToString())
                        {
                            case "pelletspreis":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), einzelpreispellets, reader.ReadString()));
                                einzelpreispellets = XmlConvert.ToDouble(reader.ReadString());
                                break;
                            case "heizoel":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), einzelpreisheizoel, reader.ReadString()));
                                einzelpreisheizoel = XmlConvert.ToDouble(reader.ReadString());
                                break;
                            case "einblaspauschale":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), einblaspauschale, reader.ReadString()));
                                einblaspauschale = XmlConvert.ToDouble(reader.ReadString()); <=== this is row 322
                                break;
                            case "ggvs":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), ggvs, reader.ReadString()));
                                ggvs = XmlConvert.ToDouble(reader.ReadString());
                                break;
                            case "mwstpellets":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), mwstpellets, reader.ReadString()));
                                mwstpellets = XmlConvert.ToDouble(reader.ReadString());
                                break;
                            case "mwstheizoel":
                                writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), mwstheizoel, reader.ReadString()));
                                mwstheizoel = XmlConvert.ToDouble(reader.ReadString());
                                break;
                            default:
                                writetolog(string.Format("Parameter konnte nicht zugeordnet werden : {0} | Wert: {1}", reader.Name.ToString(), reader.ReadString()));
                                break;
                        }
                    }
                }
            }
        }
        catch(Exception e)
        {
            writetolog("Error: " + e);
            return;
        }

This is my created logfileoutput:

12:07:09: einblaspauschale: from the variabledeclaration: 0 - from xml: 40

12:07:09: Error: System.FormatException: The input string is in the wrong format at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt) at System.Xml.XmlConvert.ToDouble(String s) at Webshopfiletransfer.Webshopfiletransfer.createCSVFile() in Webshopfiletransfer.cs:Row 322.

I am confuse, the value from the xml is available and would printed on the correct tag but the convertion to double seems to be wrong.

Is maybe anybody able to give me a tip where my misunderstanding is?

5
  • 2
    XmlConvert.ToDouble("40") works fine in .net-core can you please debug the program and check what reader.ReadString() returns? Commented Jul 30, 2020 at 11:30
  • You ReadString() for logging once, and then ReadString() the next value for processing. Commented Jul 30, 2020 at 11:31
  • It returns the string 40 Commented Jul 30, 2020 at 11:33
  • Should I better use reader.Value.ToString() instead of ReadString()? Commented Jul 30, 2020 at 11:41
  • Nevermind, Read() advances the reader, ReadString() does not. Commented Jul 30, 2020 at 11:46

1 Answer 1

0

You have two problems: The first is that ReadString() seams to return the content once but you try to read it twice. So just do var str = reader.ReadString() and use str to refer to the content.

But You could also your ReadContentAsDouble instead of converting it yourself.

The second problem is that the converions is in the wrong order. You need to do

var str = reader.ReadString();
einblaspauschale = XmlConvert.ToDouble(str);
writetolog(string.Format("{0}: gepeichert: {1} - aus xml: {2}", reader.Name.ToString(), einblaspauschale, str));

so that the logging output can be

einblaspauschale: gepeichert: 40 - aus xml: 40
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.