Click here to Skip to main content
15,885,171 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<pre lang="xml">
<GeocodeResponse>
<status>OK</status>
<result>
<type>subway_station</type>
<type>train_station</type>
<type>transit_station</type>
<type>establishment</type>
<formatted_address>
Golf Course, Captain Shashi Kant Sharma Marg, Noida Golf Course, Sector 37, Noida, Uttar Pradesh 201303, India
<formatted_address>
<address_component>
<long_name>Golf Course</long_name>
<short_name>Golf Course</short_name>
<type>subway_station</type>
<type>train_station</type>
<type>transit_station</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Captain Shashi Kant Sharma Marg</long_name>
<short_name>Captain Shashi Kant Sharma Marg</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Noida Golf Course</long_name>
<short_name>Noida Golf Course</short_name>
<type>sublocality_level_2</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Sector 37</long_name>
<short_name>Sector 37</short_name>
<type>sublocality_level_1</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Noida</long_name>
<short_name>Noida</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gautam Buddh Nagar</long_name>
<short_name>Gautam Buddh Nagar</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Uttar Pradesh</long_name>
<short_name>UP</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>201303</long_name>
<short_name>201303</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>28.5671746</lat>
<lng>77.3459920</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>28.5658059</lat>
<lng>77.3445952</lng>
</southwest>
<northeast>
<lat>28.5685039</lat>
<lng>77.3472931</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>28.5665483</lat>
<lng>77.3451542</lng>
</southwest>
<northeast>
<lat>28.5677615</lat>
<lng>77.3467341</lng>
</northeast>
</bounds>
</geometry>
<partial_match>true</partial_match>
<place_id>ChIJn5CCdrjlDDkR1c3SwjoQ__A</place_id>
</result>
</GeocodeResponse>
</pre>

This is the response I am getting from api but problem is the index of city and zip code is always changing
my code is
<pre lang="c#">
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + completeAdd + "&sensor=false";
            WebRequest request = WebRequest.Create(url);
            using (WebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    DataSet dsResult = new DataSet();
                    dsResult.ReadXml(reader);
                    DataTable dtCoordinates = new DataTable();
                    dtCoordinates.Columns.AddRange(new DataColumn[5] {
                        new DataColumn("Id", typeof(int)),
                        new DataColumn("Address", typeof(string)),
                        new DataColumn("Latitude",typeof(string)),
                        new DataColumn("Longitude",typeof(string)),
                        new DataColumn("City",typeof(string))
                    });
                    if (dsResult.Tables["result"] != null)
                        foreach (DataRow row in dsResult.Tables["result"].Rows)
                        {
                            var city = "";
                            if (dsResult.Tables["address_component"].Select()[4].ItemArray[0].ToString() == "political")
                            {
                                city = dsResult.Tables["address_component"].Select("result_id = " + row["result_id"].ToString())[4].ItemArray[0].ToString();
                            }
                            string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
                            DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];
                            dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"], city);
                        }
                    return dtCoordinates;
                }
            }

</pre>
Posted

1 solution

Using a DataSet to read XML is not very flexible. Try using System.Xml.Linq[^] to parse the XML instead.

C#
DataTable dtCoordinates = new DataTable();

dtCoordinates.Columns.AddRange(new[] {
    new DataColumn("Id", typeof(string)),
    new DataColumn("Address", typeof(string)),
    new DataColumn("Latitude", typeof(double)),
    new DataColumn("Longitude", typeof(double)),
    new DataColumn("City", typeof(string)),
    new DataColumn("ZipCode", typeof(string))
});

// Make sure the address parameter is properly encoded:
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + Uri.EscapeDataString(completeAdd) + "&sensor=false";

// Load the document, and find the "result" node:
XDocument document = XDocument.Load(url);
XElement result = document.Root == null ? null : document.Root.Element("result");

if (result != null)
{
    // TODO: There doesn't seem to be a "result_id" node anywhere in the XML.
    // Assuming you meant the "place_id", which is a string.
    string placeId = (string)result.Element("place_id");
    
    string formattedAddress = (string)result.Element("formatted_address");

    // TODO: I've assumed that "Noida" is the city in your example.
    string city = (string)result
        .Elements("address_component")
        .Where(ac => ac.Elements("type").Any(t => t.Value == "locality"))
        .Elements("long_name")
        .FirstOrDefault()
    ;

    string zipCode = (string)result
        .Elements("address_component")
        .Where(ac => ac.Elements("type").Any(t => t.Value == "postal_code"))
        .Elements("long_name")
        .FirstOrDefault()
    ;

    var location = result.Elements("geometry").Elements("location").FirstOrDefault();
    double? latitude = location == null ? default(double?) : (double?)location.Element("lat");
    double? longitude = location == null ? default(double?) : (double?)location.Element("lng");
    
    dtCoordinates.Rows.Add(placeId, formattedAddress, latitude, longitude, city, zipCode);
}

return dtCoordinates;
 
Share this answer
 
Comments
Vishal Pand3y 20-Apr-15 14:10pm    
Thanks Richard my approach was wrong :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900