2012-04-24 58 views
0

嗨的實例,我得到在下面的代碼object reference not set to an instance of an object註釋行的錯誤,有沒有辦法解決?對象引用不設置到對象的XDocument

private void button20_Click(object sender, EventArgs e) 
    { 
     string blabla1 = string.Format("http://localhost:8000/Service/AuthenticateUser/{0}/{1}", textBox30.Text, textBox31.Text); 
     XDocument xDoc = XDocument.Load(blabla1); 
     xDoc.Element("StudentID").Value.ToList(); // object reference not set to an instance of an object? 


     dataGridView12.DataSource = xDoc; 
    } 
+4

我們在做整個項目適合你? ;) – musefan 2012-04-24 10:13:31

+2

你可以附加XML? – 2012-04-24 10:14:39

+0

如果可能的話顯示xml – 2012-04-24 10:14:55

回答

2

當未發現xDoc.Element("StudentID"),呼籲.Value會給該異常。

你可能想

//xDoc.Element("StudentID").Value.ToList(); 
//List<string> ids = xDoc.Descendants("StudentID").Value.ToList(); 
List<string> ids = xDoc.Descendants("StudentID").Select(e => e.Value).ToList(); 

但是,假定XML不使用的命名空間。

編輯:

我嘗試返回result.StudentID;

string id = xDoc.Descendants("StudentID").Single().Value; 
+0

亨克它實際上是一個出於某種原因與StudentID問題,當我回到result.StudentID它從來沒有有周圍沒有任何XML括號一樣,當我返回一個列表,將normaly有''但只有字符串,所以你的第一個路線是正確的! – 2012-04-24 13:14:56

相關問題