2

我编写了一个单元测试,试图从 YouTube 请求的响应中提取标题节点。xml 恢复正常。但我似乎无法拔出标题,尝试多种不同的路径。谢谢你的帮助。

单元测试

HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo");
string urlContents = await getStringTask;
XmlDocument xml = new XmlDocument();
xml.LoadXml(urlContents);
XmlNodeList xnList = xml.SelectNodes("/entry/title");

请求的结果

    <?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>2013-05-11T12:01:32.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='Entertainment'/>
    <title type='text'>Chelsea Lately: Pharrell</title>
    <content type='text'>Pharrell chats about his latest animated film "Despicable Me." Plus, the hip-hop artist discusses his presidential friendships. Watch it!</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.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo/responses'/>
    <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='1935'/>
    </gd:comments>
    <media:group>
        <media:category label='Entertainment' 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://v8.cache4.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://v8.cache4.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." Plus, the hip-hop artist discusses his presidential friendships. Watch it!</media:description>
        <media:keywords/>
        <media:player url='http://www.youtube.com/watch?v=t5QAfTGVxuo&amp;feature=youtube_gdata_player'/>
        <media:thumbnail url='http://i.ytimg.com/vi/t5QAfTGVxuo/0.jpg' height='360' width='480' time='00:03:07.500'/>
        <media:thumbnail url='http://i.ytimg.com/vi/t5QAfTGVxuo/1.jpg' height='90' width='120' time='00:01:33.750'/>
        <media:thumbnail url='http://i.ytimg.com/vi/t5QAfTGVxuo/2.jpg' height='90' width='120' time='00:03:07.500'/>
        <media:thumbnail url='http://i.ytimg.com/vi/t5QAfTGVxuo/3.jpg' height='90' width='120' time='00:04:41.250'/>
        <media:title type='plain'>Chelsea Lately: Pharrell</media:title>
        <yt:duration seconds='375'/>
    </media:group>
    <gd:rating average='4.8778543' max='5' min='1' numRaters='3766' rel='http://schemas.google.com/g/2005#overall'/>
    <yt:statistics favoriteCount='0' viewCount='1571771'/>
</entry>
4

1 回答 1

3

XPath 查询失败的原因是 XML 包含名称空间。您需要使用XmlNamespaceManager

var doc = new XmlDocument();
doc.Load("http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo");
var nsm = new XmlNamespaceManager(doc.NameTable);
nsm.AddNamespace("t", "http://www.w3.org/2005/Atom");
var title = doc.SelectSingleNode("/t:entry/t:title", nsm);
Console.WriteLine(title.InnerText);

或者如果您更喜欢使用新的 XDocument:

var doc = XDocument.Load("http://gdata.youtube.com/feeds/api/videos/t5QAfTGVxuo");
var nsm = new XmlNamespaceManager(new NameTable());
nsm.AddNamespace("t", doc.Root.Name.NamespaceName);
var title = doc.XPathSelectElement("/t:entry/t:title", nsm);
Console.WriteLine(title.Value);
于 2013-05-11T16:30:33.680 回答