c# - Exception while loading xml document -


i'm developing windows phone application. while accessing bing maps, exception occurred while loading xml document "xmlexception - name cannot begin '.' character" @ line xdocument result = xdocument.load(r); please.

this code

private void locationtoaddress()     {         string url = "http://dev.virtualearth.net/rest/v1/locations/" + latitude + "," + longitude + "?o=xml&key=" + bingkey;         webclient wc = new webclient();         wc.downloadstringasync(new uri(url));         wc.downloadstringcompleted += wc_downloadstringcompleted;     }      void wc_downloadstringcompleted(object sender, downloadstringcompletedeventargs e)     {         string s = e.result;         xmlreader r = xmlreader.create(new memorystream(unicodeencoding.unicode.getbytes(s)));         xdocument result = xdocument.load(r);         var abc= result.root.getdefaultnamespace();         var address1 = query in result.descendants(abc + "location")                        select new location                        {                            address = (string)query.element(abc + "name")                        };         location loc = new location();         messagebox.show(loc.address);                 } 

this value of s get

<?xml version="1.0" encoding="utf-8"?><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 © 2013 microsoft , suppliers. rights reserved. api cannot accessed , content , results may not used, reproduced or transmitted in manner without express written permission microsoft corporation.</copyright><brandlogouri>http://dev.virtualearth.net/branding/logo_powered_by.png</brandlogouri><statuscode>200</statuscode><statusdescription>ok</statusdescription><authenticationresultcode>validcredentials</authenticationresultcode><traceid>a017c5068e364703bc8bdbb58f508d96|sinm001009|02.00.139.700|sinmsnvm001117, 32.151</traceid><resourcesets><resourceset><estimatedtotal>5</estimatedtotal><resources><location><name>nh-49, sampatti puram, madurai, india</name><point><latitude>9.9334483</latitude><longitude>78.0918096</longitude></point><boundingbox><southlatitude>9.9334153</southlatitude><westlongitude>78.0918519</westlongitude><northlatitude>9.9339407</northlatitude><eastlongitude>78.0909383</eastlongitude></boundingbox><entitytype>address</entitytype><address><addressline>nh-49</addressline><admindistrict>tamil nadu</admindistrict><admindistrict2>madurai</admindistrict2><countryregion>india</countryregion><formattedaddress>nh-49, sampatti puram, madurai, india</formattedaddress><locality>madurai</locality></address><confidence>medium</confidence><matchcode>good</matchcode><geocodepoint><latitude>9.9334483</latitude><longitude>78.0918096</longitude><calculationmethod>interpolation</calculationmethod><usagetype>display</usagetype><usagetype>route</usagetype></geocodepoint></location><location><name>nh-49, sampatti puram, madurai, india</name><point><latitude>9.9334153</latitude><longitude>78.0918519</longitude></point><boundingbox><southlatitude>9.9332373</southlatitude><westlongitude>78.0920988</westlongitude><northlatitude>9.9334153</northlatitude><eastlongitude>78.0918519</eastlongitude></boundingbox><entitytype>address</entitytype><address><addressline>nh-49</addressline><admindistrict>tamil nadu</admindistrict><admindistrict2>madurai</admindistrict2><countryregion>india</countryregion><formattedaddress>nh-49, sampatti puram, madurai, india</formattedaddress><locality>madurai</locality></address><confidence>medium</confidence><matchcode>good</matchcode><geocodepoint><latitude>9.9334153</latitude><longitude>78.0918519</longitude><calculationmethod>interpolation</calculationmethod><usagetype>display</usagetype><usagetype>route</usagetype></geocodepoint></location><location><name>bethaniapuram, madurai, india</name><point><latitude>9.9334153</latitude><longitude>78.0918519</longitude></point><boundingbox><southlatitude>9.9334153</southlatitude><westlongitude>78.0918519</westlongitude><northlatitude>9.9345254</northlatitude><eastlongitude>78.0910617</eastlongitude></boundingbox><entitytype>address</entitytype><address><addressline>bethaniapuram</addressline><admindistrict>tamil nadu</admindistrict><admindistrict2>madurai</admindistrict2><countryregion>india</countryregion><formattedaddress>bethaniapuram, madurai, india</formattedaddress><locality>madurai</locality></address><confidence>medium</confidence><matchcode>good</matchcode><geocodepoint><latitude>9.9334153</latitude><longitude>78.0918519</longitude><calculationmethod>interpolation</calculationmethod><usagetype>display</usagetype><usagetype>route</usagetype></geocodepoint></location><location><name>sampatti puram, madurai, india</name><point><latitude>9.9328814</latitude><longitude>78.0917037</longitude></point><boundingbox><southlatitude>9.9328051</southlatitude><westlongitude>78.0919259</westlongitude><northlatitude>9.9328814</northlatitude><eastlongitude>78.0917037</eastlongitude></boundingbox><entitytype>address</entitytype><address><addressline>sampatti puram</addressline><admindistrict>tamil nadu</admindistrict><admindistrict2>madurai</admindistrict2><countryregion>india</countryregion><formattedaddress>sampatti puram, madurai, india</formattedaddress><locality>madurai</locality></address><confidence>medium</confidence><matchcode>good</matchcode><geocodepoint><latitude>9.9328814</latitude><longitude>78.0917037</longitude><calculationmethod>interpolation</calculationmethod><usagetype>display</usagetype><usagetype>route</usagetype></geocodepoint></location><location><name>bharathiyar street, sampatti puram, madurai, india</name><point><latitude>9.9330578</latitude><longitude>78.092027</longitude></point><boundingbox><southlatitude>9.9328051</southlatitude><westlongitude>78.0919259</westlongitude><northlatitude>9.9332373</northlatitude><eastlongitude>78.0920988</eastlongitude></boundingbox><entitytype>address</entitytype><address><addressline>bharathiyar street</addressline><admindistrict>tamil nadu</admindistrict><admindistrict2>madurai</admindistrict2><countryregion>india</countryregion><formattedaddress>bharathiyar street, sampatti puram, madurai, india</formattedaddress><locality>madurai</locality></address><confidence>medium</confidence><matchcode>good</matchcode><geocodepoint><latitude>9.9330578</latitude><longitude>78.092027</longitude><calculationmethod>interpolation</calculationmethod><usagetype>display</usagetype><usagetype>route</usagetype></geocodepoint></location></resources></resourceset></resourcesets></response> 

your problem you're converting string utf-16, though xdocument expecting utf-8.

either use right encoding:

string s = e.result; xmlreader r = xmlreader.create(new memorystream(encoding.utf8.getbytes(s))); xdocument result = xdocument.load(r); 

or more simply, use directly xdocument.parse:

string s = e.result; xdocument result = xdocument.parse(s); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -