简体   繁体   中英

XML parsing error in c# using visual studio IDE

I am Facing an Exceptional error as shown below

'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: DefaultDomain): Loaded 'C:\\windows\\system32\\mscorlib.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Windows.RuntimeHost.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Windows.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Net.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Xml.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\Data\\Programs{00C6B4E0-1F82-4D23-9C2D-A1E2386F73FB}\\Install\\parsing.DLL'. Symbols loaded. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\Microsoft.Phone.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\Microsoft.Phone.Interop.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Core.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\System.Xml.Linq.ni.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 'TaskHost.exe' (CLR C:\\windows\\system32\\coreclr.dll: Silverlight AppDomain): Loaded 'C:\\windows\\system32\\en-US\\System.Xml.debug.resources.DLL'. Module was built without symbols. A first chance exception of type 'System.Xml.XmlException' occurred in System.Xml.ni.dll An exception of type 'System.Xml.XmlException' occurred in System.Xml.ni.dll but was not handled in user code The program '[2380] TaskHost.exe' has exited with code -1 (0xffffffff).

The code used for parsing is

            string url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address=Bangalore&sensor=false";



            XDocument doc = XDocument.Parse(url1);
            var lat = doc.Descendants(XName.Get("lat", url1)).FirstOrDefault();
            result.Text = (string)lat;

I am Facing the error at line XDocument.Parse(url1); Could you please help me to overcome this exception as I am Very new to this field

XDocument.Parse(str) is parse the param string in parse method . and the XDocument.Load method means to load a file in your XAP package . but you want to load a xml file from web site. you can send request to the url and read response, then parse it. like this:

string url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address=Bangalore&sensor=false";
string content = await HttpHelper.GetResponse(url1);
XDocument doc = XDocument.Parse(content);
var lat = doc.Descendants(XName.Get("lat")).FirstOrDefault();
result.Text = (string)lat;

the getResponse method here:

public async static Task<string> GetResponse(string url)
{
    try
    {
        WebRequest request = HttpWebRequest.Create(url);
        request.ContentType = "application/xml";
        WebResponse response = await request.GetResponseAsync();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string content = reader.ReadToEnd();
        reader.Dispose();
        response.Dispose();
        return content;
    }
    catch
    {
        return "";
    }
}

try this instead

string url1 = "http://maps.googleapis.com/maps/api/geocode/xml?address=Bangalore&sensor=false";
string content = await HttpHelper.GetResponse(url1);
XDocument doc = XDocument.Parse(content);
var lat = doc.Descendants(XName.Get("lat")).FirstOrDefault();
result.Text = (string)lat;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM