Get formatted_address from XML response returned by API URL using C# and VB.Net in ASP.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago

How can I get formatted_address from this link in asp.net vb.

https://maps.google.com/maps/api/geocode/xml?latlng=22.350420000000003,91.83847&sensor=false&key=API_Key

 

        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        m_xmld = New XmlDocument
        m_xmld.Load(url)
        m_nodelist = m_xmld.SelectNodes("result")
        For Each m_node In m_nodelist
            Response.Write(m_node.Item("formatted_address").InnerText)
        Next

I think it 's not right way.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<GeocodeResponse>
  <status>OK</status>
  <result>
    <type>street_address</type>
    <formatted_address>121 Nabab Siraj Ud Daula Rd, Chittagong, Bangladesh</formatted_address>
    <address_component>
      <long_name>121</long_name>
      <short_name>121</short_name>
      <type>street_number</type>
    </address_component>
    <geometry>
      <location>
        <lat>22.3507142</lat>
        <lng>91.8385187</lng>
      </location>
      <location_type>RANGE_INTERPOLATED</location_type>
      <viewport>
        <southwest>
          <lat>22.3493652</lat>
          <lng>91.8371697</lng>
        </southwest>
        <northeast>
          <lat>22.3520632</lat>
          <lng>91.8398677</lng>
        </northeast>
      </viewport>
    </geometry>
    <place_id>
      EjMxMjEgTmFiYWIgU2lyYWogVWQgRGF1bGEgUmQsIENoaXR0YWdvbmcsIEJhbmdsYWRlc2giGhIYChQKEgn1hSLwZietMBG48H3HWA4ILBB5
    </place_id>
  </result>
  <plus_code>
    <global_code>7MJH9R2Q+59</global_code>
    <compound_code>9R2Q+59 Chittagong, Bangladesh</compound_code>
  </plus_code>
</GeocodeResponse>

 

Posted 5 months ago

Hi Nishu,

Refer below sample.

Namespaces

C#

using System.Xml;

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    string url = Server.MapPath("~/XMLFile.xml");
    XmlDocument m_xmld;
    XmlNodeList m_nodelist;
    m_xmld = new XmlDocument();
    m_xmld.Load(url);
    m_nodelist = m_xmld.SelectNodes("/GeocodeResponse/result");
    Response.Write(m_nodelist[0]["formatted_address"].InnerText.Trim());
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim url As String = Server.MapPath("~/XMLFile.xml")
    Dim m_xmld As XmlDocument
    Dim m_nodelist As XmlNodeList
    m_xmld = New XmlDocument()
    m_xmld.Load(url)
    m_nodelist = m_xmld.SelectNodes("/GeocodeResponse/result")
    Response.Write(m_nodelist(0)("formatted_address").InnerText.Trim())
End Sub

Output

121 Nabab Siraj Ud Daula Rd, Chittagong, Bangladesh