title: How to parse XML-Response in C# from Nokia-HERE Geocoding REST API
body: <p><strong>INTRO</strong></p>
  
  <p>I am writing a WinForm application in C# where I am requesting a map position (latitude/longitude) for a given address.</p>
  
  <p>I utilize the Nokia HERE Geocoding REST API and request an XML, which I then try to parse. The following API call is from the Nokia docs:</p>
  
  <p><strong>REST API CALL</strong></p>
  
  <pre><code>http://geocoder.cit.api.here.com/6.2/geocode.xml?app_id=DemoAppId01082013GAL&amp;app_code=AJKnXv84fjrb0KIHawS0Tg&amp;gen=8&amp;searchtext=425+W+Randolph+Chicago
  </code></pre>
  
  <p>This API call returns an XML stream, which looks like this (shortend):</p>
  
  <p><strong>XML RESPONSE</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns2:Search xmlns:ns2="http://www.navteq.com/lbsp/Search-Search/4"&gt;
      &lt;Response&gt;
          &lt;MetaInfo&gt;
              &lt;Timestamp&gt;2014-12-15T10:11:29.197Z&lt;/Timestamp&gt;
          &lt;/MetaInfo&gt;
          &lt;View xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SearchResultsViewType"&gt;
              &lt;ViewId&gt;0&lt;/ViewId&gt;
              &lt;Result&gt;
                  &lt;Relevance&gt;1.0&lt;/Relevance&gt;
                  &lt;MatchLevel&gt;houseNumber&lt;/MatchLevel&gt;
                  &lt;MatchQuality&gt;
                      &lt;City&gt;1.0&lt;/City&gt;
                      &lt;Street&gt;0.9&lt;/Street&gt;
                      &lt;HouseNumber&gt;1.0&lt;/HouseNumber&gt;
                  &lt;/MatchQuality&gt;
                  &lt;MatchType&gt;pointAddress&lt;/MatchType&gt;
                  &lt;Location&gt;
                      &lt;LocationId&gt;NT_krOz+rwboyk4Jvih55MwPB_425&lt;/LocationId&gt;
                      &lt;LocationType&gt;address&lt;/LocationType&gt;
                      &lt;DisplayPosition&gt;
                          &lt;Latitude&gt;41.8838692&lt;/Latitude&gt;
                          &lt;Longitude&gt;-87.6389008&lt;/Longitude&gt;
                      &lt;/DisplayPosition&gt;
                      &lt;NavigationPosition&gt;
                          &lt;Latitude&gt;41.8844719&lt;/Latitude&gt;
                          &lt;Longitude&gt;-87.6387711&lt;/Longitude&gt;
                      &lt;/NavigationPosition&gt;
                  &lt;/Location&gt;
              &lt;/Result&gt;
          &lt;/View&gt;
      &lt;/Response&gt;
  &lt;/ns2:Search&gt;
  </code></pre>
  
  <p>As I am only really interested in extracting the content within the <code>&lt;NavigationPosition&gt;</code> node, I thought I could code as follows:</p>
  
  <p><strong>C# CODE SNIPPET TO RETRIEVE XML</strong></p>
  
  <pre><code>var req = (HttpWebRequest)WebRequest.Create("http://geocoder.cit.api.here.com/6.2/geocode.xml?app_id=DemoAppId01082013GAL&amp;app_code=AJKnXv84fjrb0KIHawS0Tg&amp;gen=8&amp;searchtext=425+W+Randolph+Chicago");
  req.Method = "GET";
  
  req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0";
  req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  req.ContentType = "application/x-www-form-urlencoded";
  
  req.Timeout = 5000;
  var resp = (HttpWebResponse)req.GetResponse();
  
  var ms = new MemoryStream();
  var rs = resp.GetResponseStream();
  if (rs != null)
  {
      var buf = new byte[4096];
      int len = 0;
      while ((len = rs.Read(buf, 0, buf.Length)) &gt; 0)
      {
          ms.Write(buf, 0, len);
      }
      rs.Close();
  }
  
  var xml = new XmlDocument();
  xml.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
  
  var status = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition");
  if (status.Count == 1)
  {
  
      var lat = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Latitude");
      var lng = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Longitude");
  }
  </code></pre>
  
  <p>I had to learn that I cannot parse this particular XML this way since it contains a namespace prefix <code>(ns2)</code>. So I added the namespace manager:</p>
  
  <p><strong>ADDED NAMESPACEMANAGER</strong></p>
  
  <pre><code>XmlNamespaceManager nsmanager = new XmlNamespaceManager(xml.NameTable);
  nsmanager.AddNamespace("ns2", "http://www.navteq.com/lbsp/Search-Search/4");
  </code></pre>
  
  <p>And changed the xpath query <code>xml.SelectNodes()</code> to:</p>
  
  <p><strong>UPDATED C# CODE</strong></p>
  
  <pre><code>var status = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition", nsmanager);
  if (status.Count == 1)
  {
  
      var lat = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Latitude", nsmanager);
      var lng = xml.SelectNodes("/ns2:Search/Response/View/Result[1]/Location/NavigationPosition/Longitude", nsmanager);
  }
  </code></pre>
  
  <p><strong>MY QUESTION</strong></p>
  
  <p>Now, with the NS manager added, I am able to read and parse the XML response.</p>
  
  <p>Since I am only interested in the lat/lng values of the first result (there can be more than one result, more than one result-node), is there any way to avoid a possible overhead (introduced by the namespace manager maybe) and to access the lat/lng values more easily? Can my c# code to retrieve the XML response be rewritten to be more efficient since I don't need to parse the entire response?</p>