input string was not in the correct format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff

    #1

    input string was not in the correct format

    Hey

    ..NET 2.0

    I'm parsing an XML file and read an attribute value out of a node. This
    value ( value="0.0000") am I trying to convert to a decimal:

    decimal _value = Convert.ToDecim al(reader.GetAt tribute("value" ).ToString());

    The code above gives this error:
    "Input string was not in the correct format"

    The regional settings is Norwegian on the computer I run this app on ...

    In Norway comma are used instead of period to separate the decimals from a
    number... like for example in US 0.0000 would in Norwegian become 0,0000

    any suggestions on how to fix this?


  • Jon Skeet [C# MVP]

    #2
    Re: input string was not in the correct format

    On Jan 10, 11:25 am, "Jeff" <do...@spam.mew rote:
    .NET 2.0
    >
    I'm parsing an XML file and read an attribute value out of a node. This
    value ( value="0.0000") am I trying to convert to a decimal:
    >
    decimal _value = Convert.ToDecim al(reader.GetAt tribute("value" ).ToString());
    >
    The code above gives this error:
    "Input string was not in the correct format"
    >
    The regional settings is Norwegian on the computer I run this app on ...
    >
    In Norway comma are used instead of period to separate the decimals from a
    number... like for example in US 0.0000 would in Norwegian become 0,0000
    >
    any suggestions on how to fix this?
    Use Decimal.Parse, passing in CultureInfo.Inv ariantCulture, e.g.

    decimal _value = decimal.Parse(r eader.GetAttrib ute("value"),
    CultureInfo.Inv ariantCulture);

    Jon

    Comment

    • Marc Gravell

      #3
      Re: input string was not in the correct format

      Not tested, but perhaps:
      reader.MoveToAt tribute("value" );
      reader.ReadCont entAsDecimal();

      Marc


      Comment

      • Mihai N.

        #4
        Re: input string was not in the correct format

        I'm parsing an XML file and read an attribute value out of a node. This
        value ( value="0.0000") am I trying to convert to a decimal:
        ....
        any suggestions on how to fix this?
        Jon is right, CultureInfo.Inv ariantCulture is the solution in this case.
        Because the thing is in an XML, it should be locale-independent.
        So make sure to always use CultureInfo.Inv ariantCulture, both to
        create the XML and to read it.
        And you have to do the same not only for numbers, but also for dates and
        times (and convert them to UTC times), if you have any such data.


        --
        Mihai Nita [Microsoft MVP, Windows - SDK]
        Best internationalization practices, solving internationalization problems.

        ------------------------------------------
        Replace _year_ with _ to get the real email

        Comment

        • Marc Gravell

          #5
          Re: input string was not in the correct format

          Actually, just glancing through the object library - there is a
          wrapper that encapsulates all the xml rules for the different types
          (it is used internally by XmlReader) - XmlConvert.

          i.e.

          decimal _value = XmlConvert.ToDe cimal(reader.Ge tAttribute("val ue"));

          Not only does this make it clear that you are talking about xml
          formats (and apply the correct rules automatically), but it works for
          those types that aren't stored verbatim - for instance it looks like
          TimeSpan has a non-trivial implementation.

          Marc

          Comment

          Working...