About/Contact

Steve Trefethen

Steve Trefethen is a Director of Engineering at Reply. Contact me

View my LinkedIn profile


Powered by discountASP.NET
referal ID: sdtref
Why recommend discountASP.NET?
$720 in referrals so far!


Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Disclaimer

The posts on this weblog are provided AS IS with no warranties, and confer no rights. The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.



Using Google Maps for Geocoding in C# Part 2: Deserializing to a class

December 02 2008 3:32PM

In my previous post, I illustrated how to geocode a city state combination using Google’s Map API and a comment was asked why not deserialize to a class. Well, I could have done that but in the app I’m playing around with that wasn’t really necessary although there is more useful data than just the coordinates so here’s part two.

Once you have the XML data you can construct a C# class that would allow for deserializing to an object. Generating the C# classes is straight forward and requires you to run XSD.EXE (installed with VS.NET) on the XML file to generate an .xsd file. Next, run XSD.EXE again on the .xsd with /classes to generate C# classes. Below is the output from the command line of XSD.EXE and you’ll notice I got an error the first time around (for the work around keep reading):

[c:\temp]"\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin"\xsd geo.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\temp\geo.xsd'.

[c:\temp]"\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin"\xsd geo.xsd /cl
asses
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Schema validation warning: The 'urn:oasis:names:tc:ciq:xsdschema:xAL:2.0:Address
Details' element is not declared. Line 23, position 22.

Warning: Schema could not be validated. Class generation may fail or may produce
 incorrect results.

Error: Error generating classes for schema 'geo'.
  - The element 'urn:oasis:names:tc:ciq:xsdschema:xAL:2.0:AddressDetails' is mis
sing.

If you would like more help, please type "xsd /?".

To work around the above error I deleted the value of the xmlns attribute on the <AddressDetails> node then reran the above commands resulting in this C# file.

Finally, to deserialize the XML I used the following code which is a modified version of the function from my previous post (with minimal error checking I might add). Btw, here is a page that discusses .NET XML serialization:

private string GeoCode(string city, string state, string country, string zip)
{
    string url = "http://maps.google.com/maps/geo?output=xml&key=<your_API_key>&q=" + System.Web.HttpUtility.UrlEncode(city + " " + state);
    WebRequest req = HttpWebRequest.Create(url);
    WebResponse res = req.GetResponse();
    StreamReader sr = new StreamReader(res.GetResponseStream());
    try
    {
        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(kml));
        using (System.IO.StringReader reader = new System.IO.StringReader(sr.ReadToEnd()))
        {
            kml item = (kml)serializer.Deserialize(reader);
            if(item.Items.Count() > 0 && item.Items[0].Placemark.Count() > 0 && item.Items[0].Placemark[0].Point.Count() > 0)
                return item.Items[0].Placemark[0].Point[0].coordinates;
            else 
              return "";
        }
    }
    finally
    {
        sr.Close();
    }
}
FacebookDel.icio.usDigg It!

Comments (5) -

12/9/2008 5:03:31 PM #

Just a heads up, your webserver doesnt serve up the .cs file.

You might want to consider changing the extension.

Fritz

12/10/2008 3:16:59 PM #

Hi Fritz,
  Thanks for the heads up I'll get it fixes.

Steve Trefethen

11/5/2009 11:05:42 AM #

Any idea why adressdetail is being null ?!?! It is in the Xml that is returned by google..... but when I deserialize it is = null

thanks !!!

bruno Brazil

12/3/2009 6:03:43 PM #

The reason is he took out the namespace of the addressDetail. I guess he is more interested on the points and the map. If you don't take the namespace out ('urn:oasis:names:tc:ciq:xsdschema:xAL:2.0:AddressDetails'), you won't get null, but make sure you created a single class from the two schemas generated for you

mekbeb United States

1/10/2010 1:40:06 PM #


Error: Error generating classes for schema 'geo'.
  - The element 'urn:oasis:names:tc:ciq:xsdschema:xAL:2.0:AddressDetails' is missing.

If you specify both of the generated XSD files when you run the xsd.exe tool to generate the classes you won't get the error.  

xsd.exe /classes "schema.xsd" "referencedSchema.xsd"

Greg United States

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading