2012-07-01 3 views
3

나는 youtube 채널의 원자 피드를 분석하고 싶다. 여기에 그 rss 원자 피드의 링크가 있습니다. http://gdata.youtube.com/feeds/api/users/cokestudio/uploads?orderby=updatedparse youtube feeds with linq

List<YTFeeds> lstYT = new List<YTFeeds>(); 
XDocument xDocumentYT = XDocument.Load(Server.MapPath("XMLFile.xml")); 
XNamespace xmlns = "http://www.w3.org/2005/Atom"; 
lstYT.AddRange((from entry in xDocumentYT.Descendants(xmlns + "entry").Elements(xmlns + "media:group") 
         select new YTFeeds 
         { 
          Title = entry.Element(xmlns + "media:title").Value, 
          Description = entry.Element(xmlns + "media:description").Value, 
          Video = entry.Elements(xmlns + "media:player").ElementAt(1).Attribute("url").Value, 
          Image = entry.Elements(xmlns + "media:thumbnail").ElementAt(1).Attribute("url").Value 

         }).ToList()); 

나는 invalid character or hexcode ":"를 말한다 오류를 얻고있다. 태그에서 요소를 가져오고 싶습니다. <media:group> 제안 해주십시오.

답변

4

네임 스페이스를 사용하여 요소의 이름을 쓸 때는 접두사를 생략해야합니다.이 접두사는 처리됩니다. 이 경우 media 요소를 가져올 수 있으려면 별도의 네임 스페이스 인스턴스가 필요합니다. 따라서 제목, 설명 등을 액세스하는 것은 다음과 같아야합니다.

var doc = XDocument.Load(Server.MapPath(@"XMLFile.xml")); 
XNamespace xmlns = "http://www.w3.org/2005/Atom"; 
XNamespace media = "http://search.yahoo.com/mrss/"; 
var query = 
    from entry in doc.Root.Elements(xmlns + "entry") 
    let grp = entry.Element(media + "group") 
    select new YTFeeds 
    { 
     Title = (string)grp.Element(media + "title"), 
     Description = (string)grp.Element(media + "description"), 
     Video = (string)grp.Element(media + "player").Attribute("url"), 
     Image = grp.Elements(media + "thumbnail") 
      .Select(e => (string)e.Attribute("url")) 
      .First(), 
    }; 
var lstYT = query.ToList(); 
+0

감사합니다. Jeff Mercado .. –