Windows Phone 8: XML Read issue -
okay im looking @ xml document (http://dev.virtualearth.net/rest/v1/locations/53.8100,-1.5500?o=xml&key=#)
its getting downloaded app correctly keep getting error here
lang = resultelements.element("resourcesets") _ .element("resourceset") _ .element("resources") _ .element("location") _ .element("address") _ .descendants("postalcode").value.tostring()
anybody know why?
the reason you're getting null reference exception because didn't handle namespace in xml document:
<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">
there's 3 namespaces, 2 of them assigned prefixes. 1 want last one:
xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"
the following code trick:
dim resultelements xdocument = xdocument.load("http://dev.virtualearth.net/rest/v1/locations/53.8100,-1.5500?o=xml&key=agqtkdaecz38runibck_gotwrok3a3jlscyr9dfmkd7mrmn0t0c6g9lcay1klmv3") dim ns xnamespace = "http://schemas.microsoft.com/search/local/ws/rest/v1" dim lang = (resultelements.descendants(ns + "postalcode").firstordefault()).value
when there namespace, need prepend appropriate namespace element name - i.e., ns + "postalcode". above code snippet returns "ls2 9".
firstordefault()
return first item matches, or default value if no items match.
if expect have collection of postal codes, can remove firstordefault() , iterate through returned collection, well. this:
dim lang = resultelements.descendants(ns + "postalcode") each postalcode xelement in lang console.writeline(postalcode.value) next
Comments
Post a Comment