2014-03-04 5 views
1

YouTube 요청을 구문 분석하는 동안 문제가 있습니다. 응답에서 노드를 추출하려고하지만 코드가 항상 0을 반환합니다. 이 코드 조각입니다 : 이것은YouTube 요청 구문 분석

<?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.7' max='5' min='1' numRaters='5332' rel='http://schemas.google.com/g/2005#overall'/> 
<yt:statistics favoriteCount='0' viewCount='2085078'/> 
</entry> 

난 당신이 도움이 될 수 있도록 내 질문에 분명하다 희망 XML 파일을입니다

 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(); 
    } 

+0

LINQ to XML을 사용하지 않는 이유는 무엇입니까? 일반적으로 네임 스페이스를 사용하면 훨씬 간단합니다. –

+0

그리고 제공하신 XML은 유효하지 않습니다. ''닫는 태그가 있는데 ''여는 태그가 없습니다 ... –

+0

아무 이유도 없지만이 예제를 발견하고 쉽게 볼 수 있습니다. 이해 –

답변

3

당장의 문제는 '당신이 즉 요소 media:thumbnail을 포함하는 중간에있는 media:group 요소를 무시했습니다. 그래서 당신은 당신의 XPath를 변경할 수 있습니다

var title = xml.SelectNodes("/t1:entry/t2:group/t2:thumbnail", nsm); 

그것의 많은 당신이 진단 문제가있을 때 당신이 당신의 XML 형식을 경우이를 쉽게 볼 수 :

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

는 개인적으로 나는 XML에 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"); 
+0

항상 0을 반환합니다 –

+0

@RamiRaddaoui : 제공 한 XML은 정확하지 않아야합니다. 당신이 제공 한 * 정확한 * XML을 가져 와서''줄을 삭제하고 위의 두 쿼리 모두 4 개의 요소를 반환했습니다. –

+0

지금까지 문제가 계속되었습니다 .i 동영상의 ID를 변경했지만 아무 것도 변경되지 않았습니다. –