2012-10-30 44 views
1

我想解析的YouTube的XML飼料和循環通過某個用戶的視頻,你是怎麼做到的?我正在使用umbraco剃刀(c#)。YouTube的飼料解析XML C#剃鬚刀

@using System; 
@using System.Linq; 
@using System.Collections.Generic; 
@using System.Xml; 
@using umbraco.MacroEngines; 
@using System.Xml.Linq; 
@inherits System.Xml.XPath.XPathNodeIterator; 
@inherits umbraco.MacroEngines.DynamicNodeContext; 
@inherits DynamicNodeContext 

@{ 
    dynamic xmlv = new DynamicXml(umbraco.library.GetXmlDocumentByUrl("https://gdata.youtube.com/feeds/api/users/[username]/uploads")); 

    <ul> 
    @foreach (var property in xmlv.BaseElement.Elements.Where(x => x.Elements("Name") == "group")) 
    { 
     <li> 
      <p> 
      @property.Title 
      @property.Content 
      @property.Thumbnail 
      </p> 
     </li> 
    } 
    </ul> 
} 

回答

1
XDocument xDoc = XDocument.Load("https://gdata.youtube.com/feeds/api/standardfeeds/most_viewed"); 
XNamespace media = "http://search.yahoo.com/mrss/"; 
XNamespace yt = "http://gdata.youtube.com/schemas/2007"; 

var items = xDoc.Descendants(media + "group") 
       .Select(i => new 
       { 
        Title = i.Element(media + "title").Value, 
        Content = i.Element(media + "content").Attribute("url").Value, 
        Thumbnail = i.Element(media + "thumbnail").Attribute("url").Value, 
        Uploaded = (DateTime)i.Element(yt + "uploaded"), 
       }) 
       .ToList(); 
+0

感謝,當場。 – PeteTheGreek

+0

我注意到在YouTube媒體中有另一個字段:group元素,,你將如何訪問它? – PeteTheGreek

+0

已分類。 http://stackoverflow.com/questions/12019152/get-xml-namespace-elements-with-c-sharp – PeteTheGreek