2012-07-19 46 views
0

我可以將我的變量xmlDoc的結果保存到xml文檔(在磁盤上),並且查詢返回結果;然而,當我運行它(下面的代碼)時,查詢結果爲空。如何將提要憑證傳遞給允許Linq使用XML的XDocument

爲什麼我的查詢不能針對xmlDoc?請參閱下面的屏幕截圖,瞭解調試輸出'xmlDoc.Root'。

必須有更簡潔的方式來創建XDocument?

System.Net.WebClient wc = new System.Net.WebClient(); 

wc.Credentials = new System.Net.NetworkCredential("[email protected]", "my_password"); 

XDocument xmlDoc = XDocument.Parse(wc.DownloadString(new Uri("https://mail.google.com/mail/feed/atom")), LoadOptions.None); 



var q = (from c in xmlDoc.Descendants("entry") 

     select new 
     { 
      name = c.Element("title").Value, 
      url = c.Element("link").Attribute("href").Value, 
      email = c.Element("author").Element("email").Value 
     }).ToList(); 

q.Dump(); 

xmlDoc.Root:

screen shot of XML

回答

0

採取命名空間考慮:

XNamespace df = xmlDoc.Root.Name.Namespace; 

var q = (from c in xmlDoc.Descendants(df + "entry") 

     select new 
     { 
      name = c.Element(df + "title").Value, 
      url = c.Element(df + "link").Attribute("href").Value, 
      email = c.Element(df + "author").Element(df + "email").Value 
     }).ToList(); 
相關問題