Last post Jan 06, 2011 07:40 AM by Martin_Honnen
Jan 04, 2011 09:54 PM|jfeeney|LINK
I am working with Bing Map and I have gotten back an XML. An example of what the XML would look like would be found here
http://msdn.microsoft.com/en-us/library/ff701714.aspx about half way down the page.
I am going to put a small portion of the above on here for example purpose
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>Copyright © 2010 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
<Name>1 Microsoft Way, Redmond, WA 98052 8300</Name>
<AddressLine>1 Microsoft Way</AddressLine>
<FormattedAddress>1 Microsoft Way, Redmond, WA 98052 8300</FormattedAddress>
I was above to find this little code snipit to get the Longitude and Latitude from the above file
// from this web page
var xml = XDocument.Load(reader);
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
var points = (from p in xml.Descendants(ns + "Point")
Lat = (double)p.Element(ns + "Latitude"),
Long = (double)p.Element(ns + "Longitude")
Literal1.Text = points.Lat.ToString() + ", " + points.Long.ToString();
I have been searching (unfortuately I don't know what I am looking for) to figure out how to read the StatusCode
In the xml above the StatusCode is 200 and the StatusDescription is OK
I would like to extract the StatusCode and StatusDescription....Can someone give me the code sample and also tell me is the LinqToXML or what do you Google search to find info on reading XML using XNamespace
Thanks, John Feeney
Jan 05, 2011 07:48 AM|Martin_Honnen|LINK
If your variable named 'xml' is the XDocument with the Response XML document you have posted then you simply need
int statusCode = (int)xml.Root.Element(ns + "StatusCode");
string statusDesc = (string).xml.Root.Element(ns + "SatusDescription");
Jan 05, 2011 12:45 PM|jfeeney|LINK
Thank you, that was correct.
If I wanted to read more about the proper syntax for extracting elements from xml using Xnamespace .... what do I google....is this part of LinqtoXML or should I be looking at something else....also is the key words "read values" "parse" or somethng else?
Jan 06, 2011 07:40 AM|Martin_Honnen|LINK
If you want to learn about LINQ to XML then I suggest you start by reading the relevant sections on MSDN:
http://msdn.microsoft.com/en-us/library/bb387098.aspx. It has a section "Working with XML namespaces". That is a better approach
than starting with a Google search as that documentation is well organized and exhaustive whereas a Google search often turns up blogs entries of varying quality.