我試圖解析來自YouTube API的結果。我以字符串的形式正確地獲取結果,但無法正確解析它。使用LINQ to XML在多個命名空間中處理XML
我在前面的主題上遵循了建議,但沒有得到任何結果。
我的示例代碼:
string response = youtubeService.GetSearchResults(search.Term, "published", 1, 50);
XDocument xDoc = XDocument.Parse(response, LoadOptions.SetLineInfo);
var list = xDoc.Descendants("entry").ToList();
var entries = from entry in xDoc.Descendants("entry")
select new
{
Id = entry.Element("id").Value,
Categories = entry.Elements("category").Select(c => c.Value)
//Published = entry.Element("published").Value,
//Title = entry.Element("title").Value,
//AuthorName = entry.Element("author").Element("name").Value,
//Thumnail = entry.Element("media:group").Elements("media:thumnail").ToList().ElementAt(0)
};
foreach (var entry in entries)
{
// entry.Id and entry.Categories available here
}
的問題是,項目具有計數0即使的XDocument顯然具有有效值。
響應變量(示例XML)的值,可以在這裏看到:http://snipt.org/lWm
(FYI:YouTube的模式是列在這裏:http://code.google.com/apis/youtube/2.0/developers_guide_protocol_understanding_video_feeds.html)
誰能告訴我,我做錯了什麼這裏?
你的第一個問題是沒有發佈一個失敗的例子XML。由於你的第一個問題,我不知道你的第二個問題是什麼。 – 2009-08-03 22:17:28
也許你的第二個問題是你沒有在foreach循環中設置斷點?你嘗試過了,還是依賴於調試器? – 2009-08-03 22:19:01