2013-05-11 80 views
2

我编写了一个单元测试,试图从YouTube请求的响应中提取标题节点。 xml恢复正常。但我似乎无法取消标题,尝试多种不同的途径。谢谢你的帮助。从请求从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>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

随时享受XML的looong行...:P – 2013-05-11 16:18:25

+0

只有我试图摆脱的东西有标题。对不起,有任何困惑。 – user516883 2013-05-11 16:24:19

+0

我想如果你花了一些时间来分割和格式化xml,你会自己找到答案,或者有人可能真的想回答这个问题。 (友情提示) – 2013-05-11 16:28:15

回答

3

为什么你的XPath查询失败是因为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"); 

结果。你需要使用一个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);