2013-08-25 101 views
-1

我今天爲我的C#應用​​程序製作了XML文件。
XML(我的推杆在引擎收錄,「導致其還挺大的XML代碼,不好意思):Pastebin
和C#XML,XDocument.Parse和foreach XElement

private void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
{ 
    if (e.Error == null) 
    { 
     XDocument doc = XDocument.Parse(e.Result); 

     foreach (XElement xe in doc.Root.Element("builds").Element("build").Element("items").Elements("item")) 
     { 
      string s0 = xe.Element("name").ToString(); 
      string s1 = xe.Element("uri").ToString(); 
      string[] s2 = new string[2]; 

      s2[0] = s0; 
      s2[1] = s1; 
      ListViewItem lvi = new ListViewItem(s2); 
      listView1.Items.Add(lvi); 
     } 

    } 
} 

內部的代碼,我得到錯誤 - Object reference not set to an instance of an object.或者有時根本

其不顯示
+0

調試你的代碼。其中一個'.Element(name)'返回null。所以'null.SomeMethod'會拋出異常。 – I4V

+0

試試doc.Descendants(「name」); – Jonesopolis

+0

@ I4V調試並沒有給我任何意義 – user2713690

回答

0

在您的XML文件中沒有items節點或item節點,因此您得到空值。

+0

謝謝,但是轉換到XML文件中的'files'和'file',我仍然得到同樣的錯誤!編輯:修正。 – user2713690