locked
xml parsing RRS feed

  • General discussion

  • Its my first time that i parse an xml. I am having problems identifying the classes that i want. from all the xml i just need 3 informations ( the ones in bold and underline ). Should I create class that have all the elements or just the elements that i want ?

    How can i parse ( media:thumbnail ) because its giving me an error because i cant put ":" between words when parsing. Thank you so much. 

    <entry>
    <id>
    http://gdata.youtube.com/feeds/api/videos/OpQFFLBMCPI
    </id>
    <published>
    2013-02-05T22:00:58.000Z
    </published>
    <updated>
    2013-08-23T18:16:20.000Z
    </updated>
    <app:control>
    <yt:state name="restricted" reasonCode="limitedSyndication">
    Syndication of this video was restricted.
    </yt:state>
    </app:control>
    <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="Music" label="Music" />
    <title type="text">
    P!nk - Just Give Me A Reason ft. Nate Ruess
    </title>
    <content type="text">
    From the Grammy Nominated album The Truth About Love available now - http://smarturl.it/tal Music video by P!nk featuring Nate Ruess performing Just Give Me ...
    </content>
    <link rel="alternate" type="text/html" href="https://www.youtube.com/watch?v=OpQFFLBMEPI&feature=youtube_gdata" />
    <link rel="http://gdata.youtube.com/schemas/2007#video.responses" type="application/atom+xml"href="https://gdata.youtube.com/feeds/api/videos/OpQFFLBMEPI/responses" />
    <link rel="http://gdata.youtube.com/schemas/2007#video.related" type="application/atom+xml"href="https://gdata.youtube.com/feeds/api/videos/OpQFFLBMEPI/related" />
    <link rel="self" type="application/atom+xml" href="https://gdata.youtube.com/feeds/api/videos/OpQFFLBMEPI" />
     <author>
    <name>
    PinkVEVO
    </name>
    <uri>
    https://gdata.youtube.com/feeds/api/users/PinkVEVO
    </uri>
     </author>
    <gd:comments>
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="https://gdata.youtube.com/feeds/api/videos/OpQFFLBMEPI/comments"countHint="96163" />
    </gd:comments>
    <yt:hd />
     <media:group>
    <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">
    Music
    </media:category>
    <media:content url="https://www.youtube.com/v/OpQFFLBMEPI?version=3&f=videos&app=youtube_gdata" type="application/x-shockwave-flash"medium="video" isDefault="true" expression="full" duration="243" yt:format="5" />
    <media:description type="plain">
    From the Grammy Nominated album The Truth About Love available now - http://smarturl.it/tal Music video by P!nk featuring Nate Ruess performing Just Give Me ...
    </media:description>
    <media:keywords />
    <media:player url="https://www.youtube.com/watch?v=OpQFFLBMEPI&feature=youtube_gdata_player" />
    <media:restriction type="country" relationship="deny">
    BQ CW DE SS SX
    </media:restriction>
    <media:thumbnail url="https://i.ytimg.com/vi/OpQFFLBMEPI/0.jpg" height="360" width="480" time="00:02:01.500" />
    <media:thumbnail url="https://i.ytimg.com/vi/OpQFFLBMEPI/1.jpg" height="90" width="120" time="00:01:00.750" />
    <media:thumbnail url="https://i.ytimg.com/vi/OpQFFLBMEPI/2.jpg" height="90" width="120" time="00:02:01.500" />
    <media:thumbnail url="https://i.ytimg.com/vi/OpQFFLBMEPI/3.jpg" height="90" width="120" time="00:03:02.250" />
    <media:title type="plain">
    P!nk - Just Give Me A Reason ft. Nate Ruess
    </media:title>
    <yt:duration seconds="243" />
     </media:group>
    <gd:rating average="4.8958893" max="5" min="1" numRaters="971772" rel="http://schemas.google.com/g/2005#overall" />
    <yt:statistics favoriteCount="0" viewCount="190238055" />
     </entry>

    Saturday, August 24, 2013 6:31 AM

All replies

  • You can do it with a simple class describing the results that you want like this:

        public class Entry
        {
            public string Published { get; set; }
            public string PlayerUrl { get; set; }
            public string ThumbnailUrl { get; set; }
            public string ThumbnailHeight { get; set; }
            public string ThumbnailWidth { get; set; }
            public string ThumbnailTime { get; set; }
        }

    The elements with ":" are namespaces and the document itself is qualified with its own namespace.  This looks like a feed from YouTube so I was able to parse the results with the following.

            async Task getFeedAsync()
            {
                var client = new HttpClient();
                var response = await client.GetStringAsync("https://gdata.youtube.com/feeds/api/videos");
                var xdoc = XDocument.Parse(response);
    
                XNamespace feed = "http://www.w3.org/2005/Atom";
                XNamespace media = "http://search.yahoo.com/mrss/";
                var results = from entry in xdoc.Descendants(feed + "entry")                        
                              select new Entry
                              {                      
                                  Published = entry.Element(feed + "published").Value,                              
                                  PlayerUrl = entry.Element(media + "group").Element(media + "player").Attribute("url").Value,
                                  ThumbnailUrl = entry.Element(media + "group").Element(media + "thumbnail").Attribute("url").Value,
                                  ThumbnailHeight = entry.Element(media + "group").Element(media + "thumbnail").Attribute("height").Value,
                                  ThumbnailWidth = entry.Element(media + "group").Element(media + "thumbnail").Attribute("width").Value,
                                  ThumbnailTime = entry.Element(media + "group").Element(media + "thumbnail").Attribute("time").Value
                              };
                var Entries = results.ToList<Entry>();
            }

    Saturday, August 24, 2013 1:16 PM