2011-12-19 3 views
0

내가 내가이 방법에서 XML을 구문 분석하고 있습니다 ... 문제가 있습니다윈도우 폰 구문 분석 XML은 (같은 <media:group>가)

public MainPage(){ 
InitializeComponent(); 
WebClient client = new WebClient(); 
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); 
Uri url = new Uri("http://gdata.youtube.com/feeds/api/users/my_channel/uploads/?alt=rss&prettyprint=true", UriKind.Absolute); 
client.OpenReadAsync(url); 
} 
public void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e){ 
try{ 
    var xml=XDocument.Load(e.Result); 
    var results=from row in xml.Element("rss").Element("channel").Elements().Where(element=>{ 
     return(element.Name.LocalName == "item"); 
    })select row; 
    List<MioVideo> videoList = new List<MioVideo>(); 
    int totalevideo = 0; 
    foreach (var result in results){ 
     XElement title=result.Elements().Where(element => { 
      return element.Name.LocalName == "title"; 
     }).FirstOrDefault(); 
     XElement description = result.Elements().Where(element => 
     { 
      return element.Name.LocalName == "description"; 
     }).FirstOrDefault(); 
     XElement mylink = result.Elements().Where(element => 
     { 
      return element.Name.LocalName == "link"; 
     }).FirstOrDefault(); 
     String id_video = mylink.Value; 
     id_video = id_video.Substring(31,11); 
     videoList.Add(new MioVideo(title.Value, description.Value, mylink.Value,"http://img.youtube.com/vi/"+id_video+"/hqdefault.jpg")); 
     totalevideo++; 
    } 
    listavideo.ItemsSource = videoList; 
} 
catch (Exception c){ 
    MessageBox.Show(c.Message); 
} 
} 

내가 다른 노드를 구문 분석하고 싶지만, 그것을이있는 이름처럼, 나는 그것을 파싱하고 yt : duration 내부의 값을 검색하는 방법을 모른다.

다른 사람이 내게 줄 수 있습니까 ???

<?xml version='1.0' encoding='UTF-8'?> 
<rss xmlns:app='http://purl.org/atom/app#' xmlns:atom='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' version='2.0'> 
    <channel> 
     <description></description> 
     <atom:id>http://gdata.youtube.com/feeds/api/users/mychannel/uploads</atom:id> 
     <lastBuildDate>Tue, 03 Jan 2012 12:24:02 +0000</lastBuildDate> 
     <category domain='http://schemas.google.com/g/2005#kind'>http://gdata.youtube.com/schemas/2007#video</category> 
     <title>Uploads by MyChannel</title> 
     <image> 
      <url>http://www.youtube.com/img/pic_youtubelogo_123x63.gif</url> 
      <title>Uploads by MyChannel</title> 
      <link>http://www.youtube.com/profile?user=MyChannel#p/u</link> 
     </image> 
     <link>http://www.youtube.com/profile?user=MyChannel#p/u</link> 
     <atom:link rel='self' href='http://gdata.youtube.com/feeds/api/users/MyChannel/uploads?alt=rss&amp;start-index=1&amp;max-results=25'/> 
     <managingEditor>MyChannel</managingEditor> 
     <generator>YouTube data API</generator> 
     <openSearch:totalResults>75</openSearch:totalResults> 
     <openSearch:startIndex>1</openSearch:startIndex> 
     <openSearch:itemsPerPage>25</openSearch:itemsPerPage> 
     <item> 
      <guid isPermaLink='false'>http://gdata.youtube.com/feeds/api/videos/Qt6wF29Ytzo</guid> 
      <pubDate>Fri, 23 Dec 2011 12:10:17 +0000</pubDate> 
      <atom:updated>2011-12-23T13:22:34.000Z</atom:updated> 
      <category domain='http://schemas.google.com/g/2005#kind'>http://gdata.youtube.com/schemas/2007#video</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/categories.cat'>Comedy</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>Natale</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>paolo</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>labati</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>auguri</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>comicità</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>italiana</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>mychannel</category> 
      <category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>MyChannel</category> 
      <title>mio video</title> 
      <description>Tanti auguri di Buon Natale!!!</description> 
      <link>http://www.youtube.com/watch?v=Qt6wF29Ytzo&amp;feature=youtube_gdata</link> 
      <author>MyChannel</author> 
      <gd:comments> 
       <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/Qt6wF29Ytzo/comments' countHint='0'/> 
      </gd:comments> 
      <media:group> 
       <media:category label='Commedie' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Comedy</media:category> 
       <media:content url='http://www.youtube.com/v/Qt6wF29Ytzo?version=3&amp;f=user_uploads&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='17' yt:format='5'/> 
       <media:content url='rtsp://v4.cache4.c.youtube.com/CigLENy73wIaHwk6t1hvF7DeQhMYDSANFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='17' yt:format='1'/> 
       <media:content url='rtsp://v3.cache5.c.youtube.com/CigLENy73wIaHwk6t1hvF7DeQhMYESARFEgGUgx1c2VyX3VwbG9hZHMM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='17' yt:format='6'/> 
       <media:description type='plain'>Tanti auguri di Buon Natale!!!</media:description> 
       <media:keywords>Natale, paolo, labati, auguri, comicità, italiana, mychannel, MyChannel</media:keywords> 
       <media:player url='http://www.youtube.com/watch?v=Qt6wF29Ytzo&amp;feature=youtube_gdata_player'/> 
       <media:thumbnail url='http://i.ytimg.com/vi/Qt6wF29Ytzo/0.jpg' height='360' width='480' time='00:00:08.500'/> 
       <media:thumbnail url='http://i.ytimg.com/vi/Qt6wF29Ytzo/1.jpg' height='90' width='120' time='00:00:04.250'/> 
       <media:thumbnail url='http://i.ytimg.com/vi/Qt6wF29Ytzo/2.jpg' height='90' width='120' time='00:00:08.500'/> 
       <media:thumbnail url='http://i.ytimg.com/vi/Qt6wF29Ytzo/3.jpg' height='90' width='120' time='00:00:12.750'/> 
       <media:title type='plain'>Paolo Labati - Buon Natale</media:title> 
       <yt:duration seconds='17'/> 
      </media:group> 
      <yt:statistics favoriteCount='1' viewCount='432'/> 
     </item> 
    </channel> 
</rss> 

와 나는 YT에서 값을 검색해야합니다 : 다른 단어


, 내 XML은 기간과 미디어 : 썸네일

답변

0

에없는 요소를 구문 분석을 기본 네임 스페이스 인 경우 XNamespace 개체를 만들어야합니다. XML 문서 맨 위에는 미디어의 네임 스페이스가 있어야합니다.

은 링크에서 XML을 얻을 :

상단에
http://gdata.youtube.com/feeds/api/users/my_channel/uploads/?alt=rss&prettyprint=true 

, 그것은 정의 된 네임 스페이스를해야합니다.

xmlns:media="http://www.w3.org/1999/xhtml" 

복사 같은 프로젝트에이 네임 스페이스 : (당신이 코드에서 때마다 떨어져 그것을 분석 할 수있다) 그리고

protected XNamespace ns = "http://www.w3.org/1999/xhtml" 

, 그것을 사용하는 방법의 예 :

을 요소가 미디어 : 태그를 사용할 것으로 예상되는 경우 다음을 수행하십시오.

IEnumerable<XElement> links= responseElement.Elements(ns + "link"); 
+0

미안하지만 내 코드로 예제를 만들 수 있습니까? 나는 이해하지 못한다. 나는 wp7에 새로왔다. – Zak

+0

@Zak이 업데이트되었다. 위로 –

+0

을 찾으십시오. responseElement에 대한 오류를 검색합니다. 현재 컨텍스트에 존재하지 않습니다. – Zak