Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 解析YouTube请求_C#_Xml_Xml Parsing - Fatal编程技术网

C# 解析YouTube请求

C# 解析YouTube请求,c#,xml,xml-parsing,C#,Xml,Xml Parsing,解析youtube请求时出现问题。 我想从响应中提取节点,但我的代码总是返回零。 这是代码片段: public void parser(string id) { WebClient wc = new WebClient(); string urlContents = wc.DownloadString("http://gdata.youtube.com/feeds/api/videos/"+id); XmlDocument

解析youtube请求时出现问题。 我想从响应中提取节点,但我的代码总是返回零。 这是代码片段:

        public void parser(string id)
    {
        WebClient wc = new WebClient();
        string urlContents = wc.DownloadString("http://gdata.youtube.com/feeds/api/videos/"+id);
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(urlContents);
        var nsm = new XmlNamespaceManager(xml.NameTable);
        nsm.AddNamespace("t1", "http://www.w3.org/2005/Atom");
        nsm.AddNamespace("t2", "http://search.yahoo.com/mrss/");
        var title = xml.SelectNodes("/t1:entry/t2:thumbnail", nsm);
        Console.WriteLine(title.Count);

        Console.ReadLine();
    }
这是xml文件

    <?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<id>http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo</id>
<published>2010-07-08T03:08:21.000Z</published>
<updated>2014-03-03T02:47:19.000Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/><category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Entertainment' label='Divertissement'/>
<title type='text'>Pharrell | Chelsea Lately</title>
</content>
<link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=t5QAfTGVxuo&amp;feature=youtube_gdata'/>
<link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo/related'/>
<link rel='http://gdata.youtube.com/schemas/2007#mobile' type='text/html' href='http://m.youtube.com/details?v=t5QAfTGVxuo'/><link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo'/>
<author>
<name>ChelseaLately</name>
<uri>http://gdata.youtube.com/feeds/api/users/ChelseaLately</uri>
</author>
<gd:comments>
<gd:feedLink rel='http://gdata.youtube.com/schemas/2007#comments' href='http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo/comments' countHint='1892'/>
</gd:comments><media:group><media:category label='Divertissement' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Entertainment</media:category>
<media:content url='http://www.youtube.com/v/t5QAfTGVxuo?version=3&amp;f=videos&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='375' yt:format='5'/>
<media:content url='rtsp://r8---sn-4g57kuez.c.youtube.com/CiILENy73wIaGQnqxpUxfQCUtxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='375' yt:format='1'/>
<media:content url='rtsp://r8---sn-4g57kuez.c.youtube.com/CiILENy73wIaGQnqxpUxfQCUtxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='375' yt:format='6'/>
<media:description type='plain'>Pharrell chats about his latest animated film "Despicable Me.
</media:description>
<media:keywords/>
<media:player url='http://www.youtube.com/watch?v=t5QAfTGVxuo&amp;feature=youtube_gdata_player'/>
<media:thumbnail url='http://i1.ytimg.com/vi/t5QAfTGVxuo/0.jpg' height='360' width='480' time='00:03:07.500'/>
<media:thumbnail url='http://i1.ytimg.com/vi/t5QAfTGVxuo/1.jpg' height='90' width='120' time='00:01:33.750'/>
<media:thumbnail url='http://i1.ytimg.com/vi/t5QAfTGVxuo/2.jpg' height='90' width='120' time='00:03:07.500'/>
<media:thumbnail url='http://i1.ytimg.com/vi/t5QAfTGVxuo/3.jpg' height='90' width='120' time='00:04:41.250'/>
<media:title type='plain'>Pharrell | Chelsea Lately</media:title><yt:duration seconds='375'/></media:group>
<gd:rating average='4.8432107' max='5' min='1' numRaters='5332' rel='http://schemas.google.com/g/2005#overall'/>
<yt:statistics favoriteCount='0' viewCount='2085078'/>
</entry>

http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo
2010-07-08T03:08:21.000Z
2014-03-03T02:47:19.000Z
法雷尔|切尔西最近
切切地
http://gdata.youtube.com/feeds/api/users/ChelseaLately
游戏娱乐
法雷尔聊起了他最新的动画片《卑鄙的我》。
法雷尔|切尔西最近

我希望我的问题很清楚,这样您就可以帮助我了当前的问题是您忽略了中间的
media:group
元素,该元素包含
media:thumbnail
元素。因此,您可以将XPath更改为:

var title = xml.SelectNodes("/t1:entry/t2:group/t2:thumbnail", nsm);
如果在诊断问题时格式化XML,则更容易看到这一点:

<entry>
  ...
  <media:group>
    ...
    <media:thumbnail ... />
    <media:thumbnail ... />
    ...
  </media:group>
</entry>

您不使用LINQ to XML的原因是什么?通常使用LINQ to XML要简单得多,尤其是在名称空间中。而且您提供的XML一开始就无效-您有一个
结束标记,但没有
开始标记…没有原因,但我刚刚发现了这个示例,它看起来很容易理解是的,有一个打开节点,但是看起来我删除了它,因为文本太长…很抱歉,它总是返回zero@RamiRaddaoui:那么您提供的XML肯定不准确。我获取了您提供的确切XML,删除了
行,并且上述两个查询返回了4个元素。问题一直存在到现在。我更改了视频的id,但没有任何更改正如我所说,我给出的代码与您给出的XML配合使用。因此,要么您没有正确地使用我的代码,要么您在问题中给出的XML不具有代表性。无论如何,您都应该能够使用此答案中的建议来帮助您进一步诊断。特别是,在本地格式化XML,以便您可以查看结构。谢谢你@Jon Skeet。你说得对。我用wml来linq,效果很好
XDocument doc = XDocument.Load("http://gdata.youtube.com/feeds/api/videos/"+id);
XNamespace media = "http://search.yahoo.com/mrss/";
var thumbnails = doc.Root
                    .Elements(media + "group")
                    .Elements(media + "thumbnail");