2014-03-27 175 views
0

我面臨了異常錯誤在c#分析錯誤XML使用視覺工作室IDE

'TaskHost.exe'(CLR C:\ Windows \ System32下\ coreclr.dll:默認域):加載' C:\ WINDOWS \ SYSTEM32 \ mscorlib.ni.dll」。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Windows.RuntimeHost.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Windows.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Net.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Xml.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ Data \ Programs {00C6B4E0-1F82-4D23-9C2D-A1E2386F73FB} \ Install \ parsing.DLL'。符號加載。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ Microsoft.Phone.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ Microsoft.Phone.Interop.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Core.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ System.Xml.Linq.ni.dll'。跳過的加載符號。模塊已經過優化,調試器選項「Just My Code」已啓用。 'TaskHost.exe'(CLR C:\ windows \ system32 \ coreclr.dll:Silverlight AppDomain):加載'C:\ windows \ system32 \ en-US \ System.Xml.debug.resources.DLL'。模塊沒有符號。 System.Xml.ni.dll中發生類型'System.Xml.XmlException'的第一次機會異常 System.Xml.ni.dll中發生類型'System.Xml.XmlException'的異常,但未在用戶中處理代碼 程序'[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); 請幫我解決這個例外,因爲我對這個領域很陌生

+1

Insetad,你試圖解析URL自身 - 無法工作。 – Robert

+0

我也使用了XDocument.Load(url1)方法,但它仍然拋出了相同的異常。您能否給我提供正確的解析方法/代碼,謝謝 – dheeraj

+0

請參閱[this](http://stackoverflow.com/questions/19370101/如何解析xml-using-c) – LakshmiNarayanan

回答

2

XDocument.Parse(str)解析參數string in parse method。而XDocument.Load方法意味着加載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 ""; 
    } 
} 
+0

@chris ...我很擅長穿線概念 – dheeraj

+0

抱歉,但是您的意思是? –

+0

等待運算符用於多線程對嗎? – dheeraj

0

嘗試從您的URL1變量的URL獲取XML的這個代替

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; 
+0

歡迎來到Stack Overflow!請考慮包含一些關於您的答案的信息,而不是簡單地發佈代碼。我們嘗試提供的不僅僅是「修復」,而是幫助人們學習。你應該解釋原始代碼中的錯誤,你做了什麼不同,以及爲什麼你的改變起作用。 –