Pregunta

Given the xml from the request (with XXX replaced by whatever user wanted), cant really figure out why it's not returning 2 test objects to the list as it should.

String YouTubeXML = new WebClient().DownloadString("http://gdata.youtube.com/feeds/api/users/XXXXXXXXX/uploads?orderby=published");
XDocument xDoc = XDocument.Parse(YouTubeXML);

List<dynamic> videos = (from i in xDoc.Descendants("entry")
                        select new
                        {
                            //Just declaring something random
                            Title = i.Element("id").Value

                        }).ToList<dynamic>();

And the XML structure looks something like this:

<feed xmlns="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">
<id>http://gdata.youtube.com/feeds/api/users/XXXXXXXX/uploads</id>
<author>
  <name>XXXXXXXX</name>
  <uri>http://gdata.youtube.com/feeds/api/users/XXXXXXXX</uri>
</author>
<generator version="2.1" uri="http://gdata.youtube.com">YouTube data API</generator>
<openSearch:totalResults>5</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
  <id>http://gdata.youtube.com/feeds/api/videos/video1</id>
  <published>date</published>
  <updated>date</updated>
  <author>
    <name>name</name>
    <uri>http://gdata.youtube.com/feeds/api/users/XXXXXXX</uri>
  </author>
  <yt:hd />
  <media:group>
    <yt:duration seconds="179" />
  </media:group>
  <gd:rating average="4.703704" max="5" min="1" numRaters="81" rel="http://schemas.google.com/g/2005#overall" />
  <yt:statistics favoriteCount="0" viewCount="6004" />
</entry>
<entry>
  <id>http://gdata.youtube.com/feeds/api/videos/video2</id>
  <published>date</published>
  <updated>date</updated>
  <author>
    <name>name</name>
<uri>http://gdata.youtube.com/feeds/api/users/XXXXXXX</uri>
  </author>
  <yt:hd />
  <media:group>
    <yt:duration seconds="179" />
  </media:group>
  <gd:rating average="4.703704" max="5" min="1" numRaters="81"         rel="http://schemas.google.com/g/2005#overall" />
  <yt:statistics favoriteCount="0" viewCount="6004" />
</entry>
</feed>

What could possible be going wrong? The XML is being received and parsed, so it's the query that is not working.

¿Fue útil?

Solución

You're missing the fact, that XML uses namespaces, so your query has to use namespaces too:

var ns = XNamespace.Get("http://www.w3.org/2005/Atom");
var videos = (from i in xDoc.Root.Elements(ns + "entry")
              select new
              {
                  Title = (string)i.Element(ns + "id")
              }).ToList();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top