使用visual studio IDE在c#中解析XML错误

本文关键字:XML 错误 visual studio IDE 使用 | 更新日期: 2023-09-27 17:54:05

我正面临如下所示的异常错误

'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: DefaultDomain): Loaded 'C:'windows'system32'mscorlib.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Windows.RuntimeHost.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Windows.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Net.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Xml.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain): Loaded 'C:'Data'Programs{00C6B4E0-1F82-4D23-9C2D-A1E2386F73FB}'Install' parse .dll '。加载符号。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'Microsoft.Phone.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'Microsoft.Phone.Interop.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Core.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'System.Xml.Linq.ni.dll'。跳过加载符号。模块被优化,调试器选项"只是我的代码"被启用。'TaskHost.exe' (CLR C:'windows'system32'coreclr.dll: Silverlight AppDomain):加载'C:'windows'system32'en-US'System.Xml.debug.resources.DLL'。模块没有任何符号。类型为"System.Xml"的第一次偶然异常。在System.Xml.ni.dll中发生XmlException'类型为System.Xml的异常。XmlException'在System.Xml.ni.dll中发生,但未在用户代码中处理程序'[2380]TaskHost.exe'已退出,代码为-1 (0xffffffff)。

用于解析的代码是
            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;

我面对的错误行XDocument.Parse(url1);你能帮我克服这个例外吗,因为我是这个领域的新手

使用visual studio IDE在c#中解析XML错误

XDocument.Parse(str)是解析参数string in parse method。和XDocument。加载方式是指加载a file in your XAP package。但是您想从网站加载XML文件。您可以向url发送请求并读取响应,然后对其进行解析。这样的:

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;

getResponse方法:

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

试试这个

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;