0

i'm having a trouble with taking information from xml using linq in c# wpf application. followings are the codes i use.

public class YouTubeInfo
{
    public string LinkUrl { get; set; }
    public string EmbedUrl { get; set; }
    public string ThumbNailUrl { get; set; }
    public string Title { get; set; }
    public int Duration { get; set; }
}

public class YouTubeProvider 
{
    const string SEARCH = "http://gdata.youtube.com/feeds/api/videos?q={0}&alt=rss&start-index={1}&max-results={2}&v=1";
    const string Most_popular = "http://gdata.youtube.com/feeds/api/standardfeeds/KR/most_popular?time=today&alt=rss&start-index={1}&max-results={2}&v=2";
    //const string Entertainment = "https://gdata.youtube.com/feeds/api/standardfeeds/KR/most_popular_Entertainment?start-index=1&max-results=2";

    #region Load Videos From Feed
    public static int search_based;
    static string search;


    public static List<YouTubeInfo> LoadVideosKey(string keyWord, int start, int limit)
    {
        try
        {
            switch (search_based)
            {
                case 0: search = SEARCH; break;
                case 1: search = Most_popular; break;
            }
            var xraw = XElement.Load(string.Format(search, keyWord, start, limit));
            var xroot = XElement.Parse(xraw.ToString());
            var links = (from item in xroot.Element("channel").Descendants("item")
                         select new YouTubeInfo
                         {
                             LinkUrl = item.Element("link").Value,
                             Title = item.Element("title").Value,
                             EmbedUrl = GetEmbedUrlFromLink(item.Element("link").Value),
                             ThumbNailUrl = GetThumbNailUrlFromLink(item),
                             Duration = GetDuration(item),
                         }).Take(limit);

            return links.ToList<YouTubeInfo>();
        }
        catch (Exception e)
        {
            Trace.WriteLine(e.Message, "ERROR");
        }
        return null;
    }

i want to take information from this xml

https://gdata.youtube.com/feeds/api/standardfeeds/KR/most_popular_Entertainment?start-index=1&max-results=2

mdisibio
  • 3,148
  • 31
  • 47
kimtk
  • 27
  • 7
  • 1
    There is a lot of unnecessary data in your question. Post the XML (document, not link to it), the query and expected result/what's wrong with your current code. – MarcinJuraszek Feb 19 '14 at 03:27

2 Answers2

0

Your basic code is actually working fine. You did not post code for GetThumbnailUrlFromLink and for GetDuration, but I suspect you are having trouble with namespaces. See this answer for a sample of using namespaces.

Essentially, if you added:

static XNamespace nsMedia = "http://search.yahoo.com/mrss/";
static XNamespace nsYt = "http://gdata.youtube.com/schemas/2007";

then your Duration could look like:

Duration = (int)item.Element(nsMedia + "group").Element(nsYt + "duration").Attribute("seconds")
Community
  • 1
  • 1
mdisibio
  • 3,148
  • 31
  • 47
  • thanks for your comments. i'm not quiet good at this site.. when i writed xml doc, this editer converted xml to nothing. anyway, i will keep in mind your general comment! – kimtk Feb 19 '14 at 09:42
0

Maybe better if you use SyndicationFeed. See the sample below:

Import needed namspaces

using System.ServiceModel.Syndication;
using System.Xml;

Load feed implementation

private static string GetAttributeFromGroup(SyndicationElementExtensionCollection seec, string elementName, string attributeName) 
{
    foreach (SyndicationElementExtension extension in seec)
    {
        XElement element = extension.GetObject<XElement>();
        if (element.Name.LocalName == "group") 
        {
            foreach (var item in  element.Elements())
            {
                if (item.Name.LocalName == elementName) 
                {
                    return item.Attribute(attributeName).Value;
                }
            }
        }
    }
    return null;
}

public static List<YouTubeInfo> LoadVideosKey(string keyWord, int start, int limit)
{
    try
    {
        switch (search_based)
        {
            case 0: search = SEARCH; break;
            case 1: search = Most_popular; break;
        }
        var xDoc = XmlReader.Create(string.Format(search, keyWord, start, limit));
        SyndicationFeed feed = SyndicationFeed.Load(xDoc);
        var links = (from item in feed.Items
                     select new YouTubeInfo
                     {
                         LinkUrl = item.Id,
                         Title = item.Title.Text,
                         EmbedUrl = item.Links.FirstOrDefault().Uri.AbsoluteUri,
                         ThumbNailUrl = GetAttributeFromGroup(item.ElementExtensions, "thumbnail", "url"),
                         Duration = int.Parse(GetAttributeFromGroup(item.ElementExtensions, "duration", "seconds") ?? "0"),
                     }).Take(limit);

        return links.ToList<YouTubeInfo>();
    }
    catch (Exception e)
    {
        Trace.WriteLine(e.Message, "ERROR");
    }
    return null;
}

You can learn more about SyndicationFeed here

Alice
  • 1,255
  • 1
  • 9
  • 7