Reputation: 4332
I got this error msg
Cannot find file <?xml version="1.0" encoding="UTF-8"?>
Below is the code. How to resolve this issue? Appreciate your help.
rivate void button1_Click(object sender, RoutedEventArgs e) { string sPath = "http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"; WebClient wc = new WebClient(); wc.DownloadStringAsync(new Uri(sPath)); wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); } void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { XDocument xdoc = XDocument.Load(e.Result); XElement locationElement = xdoc.Element("GeocodeResponse").Element("result").Element("geometry").Element("location"); double latitude = (double)locationElement.Element("lat"); double longitude = (double)locationElement.Element("lng"); txtBlkLatLon.Text = latitude.ToString() + "," + longitude.ToString(); }
Upvotes: 0
Views: 1987
Reputation: 37660
Replace
XDocument xdoc = XDocument.Load(e.Result);
By
XDocument xdoc = XDocument.Parse(e.Result);
The former is trying to load data at the location specified by the string (which contains the data, not the location).
The later, is trying to directly read the data.
Upvotes: 1
Reputation: 15006
Where exactly does the exception happen?
Have you tried using XDocument.Parse for creating the XDocument from string? http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse.aspx
You should also attach the DownloadStringCompleted event handler before you call the DownloadStringAsync.
Upvotes: 0