dheeraj
dheeraj

Reputation: 1

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

Upvotes: 0

Views: 2226

Answers (2)

user4093763
user4093763

Reputation:

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;

Upvotes: 0

Chris Shao
Chris Shao

Reputation: 8231

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 "";
    }
}

Upvotes: 2

Related Questions