Jump to content



Photo
c# xml

  • Please log in to reply
3 replies to this topic

#1 sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 15 November 2012 - 02:32

So when I try to retrieve the full gps coordinates from my class I get the correct values. When I am looking at the debugger it shows the xml document contains the right values. However when I call float myVar = locationElem.Element("lat") or float myVar = float.Parse(locationElem.Element("lat").Value); it trims the value short. How do I force it to return the absolute float in the document?


#2 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 15 November 2012 - 03:07

To clarify:

string status = xDoc.Element("GeocodeResponse").Element("status").Value;
            XElement locationElem = xDoc.Element("GeocodeResponse").Element("result").Element("geometry").Element("location");
            double testLat = (double)locationElem.Element("lat");
            double testLong = (double)locationElem.Element("lng");
            float latitude = (float)float.Parse(locationElem.Element("lat").Value);
            float longitude = (float)float.Parse(locationElem.Element("lng").Value);
locationElem.Value (for example) = 22.01919203 but the float is stored as 22.0191...o_O? What can I do to get the full 22.0191203?

#3 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 15 November 2012 - 20:08

That's probably because floats don't have enough precision to store that many significant decimal places. You should use a decimal instead. Btw you should always specify CultureInfo.InvariantCulture to those Parse methods otherwise they will fail on any machine that has a different decimal separator.

#4 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 16 November 2012 - 13:40

Thanks Dr_Asik :) helpful as always. +1 for you (I think there is an up vote button on here? I'll find it :) ).
EDIT: There isn't one for an individual post so I rated you 5 stars on your profile. I was banging my head on my desk about this one. Google even shows an example using floats so I was a bit confused as to why my example wasn't working like theirs was. I'm going to try the decimal idea though. Lets hope for the best!