Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

input string was not in the correct format

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff - 10 Jan 2008 11:25 GMT
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.ToDecimal(reader.GetAttribute("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] - 10 Jan 2008 11:31 GMT
> .NET 2.0
>
[quoted text clipped - 12 lines]
>
> any suggestions on how to fix this?

Use Decimal.Parse, passing in CultureInfo.InvariantCulture, e.g.

decimal _value = decimal.Parse(reader.GetAttribute("value"),
CultureInfo.InvariantCulture);

Jon
Marc Gravell - 10 Jan 2008 11:36 GMT
Not tested, but perhaps:
reader.MoveToAttribute("value");
reader.ReadContentAsDecimal();

Marc
Mihai N. - 12 Jan 2008 03:32 GMT
> 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.InvariantCulture 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.InvariantCulture, 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.

Signature

Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email

Marc Gravell - 12 Jan 2008 09:42 GMT
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.ToDecimal(reader.GetAttribute("value"));

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.