2010-11-08 53 views
2

我試圖從this site中獲取所有<entry>標籤。它會很好地加載到XDocument中,但我似乎無法獲取任何<entry>元素。下面是我的代碼有:從XDocument獲取後代

XDocument netflixPage = new XDocument(); 
netflixPage = XDocument.Load("http://odata.netflix.com/Catalog/Titles"); 

foreach (XElement xe in netflixPage.Descendants("entry").ToList()) 
{ 
    string movieInfo = xe.Value; 
} 

回答

5

你的代碼試圖以檢索 命名空間entry元素,但文檔包含http://www.w3.org/2005/Atom命名空間entry元素:

XNamespace atom = "http://www.w3.org/2005/Atom"; 

XDocument doc = XDocument.Load("http://odata.netflix.com/Catalog/Titles"); 

foreach (XElement xe in doc.Descendants(atom + "entry")) 
{ 
    string movieInfo = (string)xe; 
} 
+0

謝謝您的回覆,那非常棒!現在,我試圖抓住一些更多的信息,如。我試過這個代碼: XNamespace msft =「http://schemas.microsoft.com/ado/2007/08/dataservices」; XDocument netflixPage = new XDocument(); netflixPage = XDocument.Load(「http://odata.netflix.com/Catalog/Titles」); foreach(netflixPage.Descendants(msft +「d」)中的XElement xe) { string stuff = xe.Value; } 但它不會加載。你知道如何抓住元素嗎? – 2010-11-08 18:51:51

+0

@ user500975:'d'是元素的前綴,而不是名稱。你可以忽略它。元素的名稱是'http:// schemas.microsoft.com/ado/2007/08/dataservices'命名空間中的'ReleaseYear'。你可以使用'msft +「ReleaseYear」'來訪問它,其中'XNamespace msft =「http://schemas.microsoft.com/ado/2007/08/dataservices」;' – dtb 2010-11-08 19:27:22