gpt4 book ai didi

c# - 如何从 xml 中获取数据,通过 linq,c#

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:02 24 4
gpt4 key购买 nike

你好,

我在从 youtube xml 获取数据时遇到问题:youtube xml地址:http://gdata.youtube.com/feeds/api/videos?q=keyword&orderby=viewCount

我试了一下,但是程序没有进入linq查询。

key = @"http://gdata.youtube.com/feeds/api/videos?q="+keyword+@"&orderby=viewCount";
youtube = XDocument.Load(key);
urls = (from item in youtube.Elements("feed")
select new VideInfo
{
soundName = item.Element("entry").Element("title").ToString(),
url = item.Element("entry").Element("id").ToString(),
}).ToList<VideInfo>();

谁有想法,如何解决这个问题?谢谢!

最佳答案

在 Linq to XML 中搜索元素需要您完全符合命名空间。在这种情况下:

var keyword = "food";
var key = @"http://gdata.youtube.com/feeds/api/videos?q="+keyword+@"&orderby=viewCount";
var youtube = XDocument.Load(key);
var urls = (from item in youtube.Elements("{http://www.w3.org/2005/Atom}feed")
select new
{
soundName = item.Element("{http://www.w3.org/2005/Atom}entry").Element("{http://www.w3.org/2005/Atom}title").ToString(),
url = item.Element("{http://www.w3.org/2005/Atom}entry").Element("{http://www.w3.org/2005/Atom}id").ToString(),
});
foreach (var t in urls) {
Console.WriteLine(t.soundName + " " + t.url);
}

适合我。为避免写出命名空间,一种选择是按本地名称搜索(例如 youtube.Elements().Where(e => e.LocalName == "feed")。我不确定如果有更优雅的方式来“与命名空间无关”。

关于c# - 如何从 xml 中获取数据,通过 linq,c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19742664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com