1 vote
closed
decimal.Parse fails for latitude and longitude on non-US cultures (Was: StringReader should not be used when reading UTF-8 XML)

Description

 
How to fix this problem:

Replace: decimal.Parse(reader.Value);
With: decimal.Parse(reader.Value, System.Globalization.NumberFormatInfo.InvariantInfo);


--------------
There is a problem with the Deserialize method of the Util.cs file. The method reads the input XML text into an StringReader object, which only does UTF-16. This makes the deserialization fail on certain occasions when the return value contains non-ANSI characters. When I search for "Norway", the resultset is not able to parse.

Here is the result set that won't deserialize properly when StringReader is used:

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photos page="1" pages="105769" perpage="10" total="1057684">
<photo id="4063981531" owner="28824814@N08" secret="977050a122" server="3495" farm="4" title="rafting Sjoa Norway" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 08:00:07" datetakengranularity="0" dateupload="1257091207" iconserver="2439" iconfarm="3" license="0" ownername="Rien Westerhout" originalsecret="87b3ac676a" originalformat="jpg" lastupdate="1257091833" tags="norway video rafting noorwegen sjoa" latitude="0" longitude="0" accuracy="0" machine_tags="" o_width="854" o_height="480" views="0" media="video" media_status="ready" />
<photo id="4063956443" owner="43272983@N08" secret="eac5721450" server="2726" farm="3" title="Fredheim skole." ispublic="1" isfriend="0" isfamily="0" datetaken="2009-10-27 19:21:52" datetakengranularity="0" dateupload="1257090802" iconserver="3499" iconfarm="4" license="0" ownername="zimort" lastupdate="1257091620" tags="" latitude="60.811441" longitude="10.662231" accuracy="7" place_id="iTqPk2mYA5lIAHDcPA" woeid="12592690" machine_tags="" views="0" media="photo" media_status="ready" />
<photo id="4064685616" owner="43272983@N08" secret="2477c7c3f2" server="2804" farm="3" title="Løiten, Norway" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 14:33:58" datetakengranularity="0" dateupload="1257090489" iconserver="3499" iconfarm="4" license="0" ownername="zimort" lastupdate="1257090660" tags="" latitude="0" longitude="0" accuracy="0" machine_tags="" views="0" media="photo" media_status="ready" />
<photo id="4064680836" owner="77273185@N00" secret="a0dd875cf3" server="2510" farm="3" title="Leading" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 12:48:19" datetakengranularity="0" dateupload="1257090413" iconserver="2631" iconfarm="3" license="0" ownername="Faisal!™" lastupdate="1257092131" tags="sky norway statue norge gull ålesund staty" latitude="62.471481" longitude="6.174379" accuracy="10" place_id="bknE8WmRBJrPmOE" woeid="856555" machine_tags="" views="46" media="photo" media_status="ready" />
<photo id="4063884227" owner="33836402@N07" secret="9b4864dac0" server="2622" farm="3" title="Banned trade in whale meat is picking up" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 13:33:39" datetakengranularity="0" dateupload="1257089619" iconserver="3100" iconfarm="4" license="0" ownername="MIRIAM GODET" originalsecret="e44d43ecf0" originalformat="jpg" lastupdate="1257091026" tags="world ocean food water loss japan denmark japanese israel iceland islandia washington big support seasons mercury fat sashimi tissue report first frança science meat research dolphins future planet change government environment whale species agent whales endangered tradition humpback population import bring whaling climate warming scientists hunt dinamarca global afp whalers export tokio minke icelandic blubber whalemeat slaughtered føroyar pcbs øer fær faroese færøer faeroese miriamgodet faroéislands ilhasfaroé ddtanddieldrin importexportagent whalesahoy samplecetaceans humanhun prfirminjapan jobjapan" latitude="0" longitude="0" accuracy="0" machine_tags="" o_width="200" o_height="160" views="1" media="photo" media_status="ready" />
<photo id="4063847601" owner="51129938@N00" secret="edee3da932" server="2473" farm="3" title="October 09" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-10-27 12:17:39" datetakengranularity="0" dateupload="1257088995" iconserver="55" iconfarm="1" license="0" ownername="Penny C" lastupdate="1257090001" tags="park tree fall leaves oslo norway garden october carlbernersplass" latitude="0" longitude="0" accuracy="0" machine_tags="" views="0" media="photo" media_status="ready" />
<photo id="4063842451" owner="51129938@N00" secret="6d9a4737d6" server="3496" farm="4" title="October 09" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-10-27 15:31:30" datetakengranularity="0" dateupload="1257088906" iconserver="55" iconfarm="1" license="0" ownername="Penny C" lastupdate="1257090275" tags="black leaves car oslo norway vintage volvo carlbernersplass hekkveien" latitude="0" longitude="0" accuracy="0" machine_tags="" views="0" media="photo" media_status="ready" />
<photo id="4063845157" owner="51129938@N00" secret="0b13c85c14" server="2704" farm="3" title="October 09" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-10-27 15:32:19" datetakengranularity="0" dateupload="1257088953" iconserver="55" iconfarm="1" license="0" ownername="Penny C" lastupdate="1257090154" tags="fall oslo norway carlbernersplass hekkveien" latitude="0" longitude="0" accuracy="0" machine_tags="" views="0" media="photo" media_status="ready" />
<photo id="4063835229" owner="25742691@N06" secret="241da85e8f" server="2474" farm="3" title="Autumn Shoes" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 16:19:43" datetakengranularity="0" dateupload="1257088783" iconserver="3319" iconfarm="4" license="0" ownername="JoakimK☆" lastupdate="1257092221" tags="old autumn light norway canon photography eos 350d 50mm shoes bokeh 18 leafs joakim healzo kræmer" latitude="0" longitude="0" accuracy="0" machine_tags="" views="31" media="photo" media_status="ready" />
<photo id="4063788351" owner="7345382@N06" secret="054fb7e306" server="2466" farm="3" title="Shadow of me" ispublic="1" isfriend="0" isfamily="0" datetaken="2009-11-01 13:49:01" datetakengranularity="0" dateupload="1257087954" iconserver="180" iconfarm="1" license="0" ownername="geby," originalsecret="9a5d182d7f" originalformat="jpg" lastupdate="1257092187" tags="shadow yellow norway norge nikon nikkor gul finnmark vadsø skygge varanger 247028mm" latitude="70.076848" longitude="29.725141" accuracy="14" place_id="dRdStZSRB5nBulw" woeid="865409" machine_tags="" o_width="2832" o_height="4256" views="10" media="photo" media_status="ready" />
</photos>
</rsp>

File Attachments


No files are attached


Closed Nov 4 at 8:57 AM  by samjudson

Thanks for spotting this. I've added the InvariantCulture throughout the code (for int.Parse as well).


Comments

most recent at top (show oldest at top)
CareBear wrote Nov 1 at 7:16 PM
I figured out that this problem was, it's nothing to do with encoding, but rather parsing of decimal values. I'm using Norwegian cultures on my computer and parsing "60.1234" will fail unless the System.Globalization.NumberFormatInfo.InvariantInfo is supplied to the overload. There are 6 places where decimal.Parse is being used in FlickrNet, one of them the InvariantInfo is already used, but the two other places it is not.

Properties are latitude and longitude, for example on the Photo.cs class:

Replace: decimal.Parse(reader.Value);
With: decimal.Parse(reader.Value, System.Globalization.NumberFormatInfo.InvariantInfo);

I used to get exception only once in a while, that's why it was so hard to figure out the problem ... but I realize now that the error is happening due to photos that contains cordinates.

Updating...
© 2006-2009 Microsoft | About CodePlex | Privacy Statement | Terms of Use | Code of Conduct | Advertise With Us | Version 2009.10.27.15987