解析YouTube请求

时间:2014-03-04 09:53:54

标签: 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 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>

我希望我的问题很明确,以便你能帮助我

1 个答案:

答案 0 :(得分:3)

当前的问题是您忽略了包含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:

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");