0

From the following GeoCodeRespose xml how can we extract values of locality, route and street number using xpath in a c# program.

<GeocodeResponse> 
<status>OK</status>
 <result>
<type>street_address</type>
<formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
<address_component>
<long_name>1600</long_name>
<short_name>1600</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Amphitheatre Pkwy</long_name>
<short_name>Amphitheatre Pkwy</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Mountain View</long_name>
<short_name>Mountain View</short_name>
<type>locality</type>
<type>political</type>
</address_component>
</result>
</GeocodeResponse>

so far I can get the xml in the xml document and can get the value of formatted address as below

   XmlDocument doc=GetGeoCodeXMLResponse();
   XPathDocument document = new XPathDocument(new XmlNodeReader(doc));
   XPathNavigator navigator = document.CreateNavigator();

   XPathNodeIterator resultIterator = navigator.Select("/GeocodeResponse/result");
        while (resultIterator.MoveNext())
        {

            XPathNodeIterator formattedAddressIterator = resultIterator.Current.Select("formatted_address");
            while (formattedAddressIterator.MoveNext())
            {
               string FullAddress = formattedAddressIterator.Current.Value;

            }
        }
3
  • Any reason you don't want to use an XmlSerializer or LinqtoXml? Commented Oct 12, 2013 at 23:17
  • I am open to use XmlSerializer or LinqtoXml if you can kindly give me an example to extract these kind of values from xml Commented Oct 12, 2013 at 23:19
  • Is this XML consistent every time you need to deserialize it? Commented Oct 13, 2013 at 2:27

2 Answers 2

1

If your XML is consistent, this is probably the easiest way.

Create an object that represents the XML:

public GeocodeResponse
{
  public string Status { get; set; }
  public Result Result { get; set; }

  public class Result
  {
    public string type { get; set; }
    public string formatted_address { get; set; }
    // etc..
  }
}

Deserializing an object in XML.

var serializer = new XmlSerializer(typeof(GeocodeResponse));
GeocodeResponse geocodeReponse = 
  (GeocodeResponse)serializer.Deserialize(xmlAsString);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use LinqToXml, try this, you will get an anonymous type with all the properties you need.

 var xDoc = XDocument.Parse(xmlString);

 var result = xDoc.Descendants("result").Select(c =>
              new
              {
                FormattedAddress = c.Element("formatted_address").Value,
                StreetNumber = c.Descendants("address_component")
                        .First(e => e.Element("type").Value == "street_number").Element("short_name").Value,
                Route = c.Descendants("address_component")
                        .First(e => e.Element("type").Value == "route").Element("short_name").Value,
                Locality = c.Descendants("address_component")
                        .First(e => e.Element("type").Value == "locality").Element("short_name").Value
          }).First();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.