Reputation: 58432
I'm loading an xml feed from youtube into xelement like so:
XElement element = XElement.Load(url);
This is fine and I get a document that looks like this:
<?xml version='1.0' encoding='UTF-8' ?>
<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/id/uploads</id>
<updated>2014-01-07T11:43:08.269Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/>
<title type='text'>Uploads by id</title>
<logo>http://www.gstatic.com/youtube/img/logo.png</logo>
<link rel='related' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/stonemarketuk'/>
<link rel='alternate' type='text/html' href='https://www.youtube.com/channel/333/videos'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/id/uploads'/>
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/id/uploads/batch'/>
<link rel='self' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/id/uploads?start-index=1&max-results=25'/>
<link rel='next' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/id/uploads?start-index=26&max-results=25'/>
<author>
<name>StonemarketUK</name>
<uri>https://gdata.youtube.com/feeds/api/users/id</uri>
</author>
<generator version='2.1' uri='http://gdata.youtube.com'>YouTube data API</generator>
<openSearch:totalResults>30</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
....entry elements
</entry>
<entry>
....entry elements
</entry>
</feed>
How do I get the entry elements? I have tried the following:
var entries = element.Elements("entry");
var entries = element.Element("feed").Elements("entry");
var entries = element.Nodes().Elements("entry");
But none of these return anything
I also debugged and tried element.Elements().FirstOrDefault()
but this returned null. element.Nodes().Count()
returned 41 so should I be trying to get the nodes called entry?
Upvotes: 1
Views: 857
Reputation: 338208
All elements are in a namespace (xmlns='http://www.w3.org/2005/Atom'
). You must declare the atom namespace and use it.
It's easy:
XNamespace atom = "http://www.w3.org/2005/Atom";
var xml = XElement.Load(url);
var entry = xml.Elements(atom + "entry");
etc.
An easy way to get the right Element name is using XName.Get()
. If you only have one namespace, you could put it in a seperate function:
internal static XName GetXName(string name)
{
string atomNamespace = "http://www.w3.org/2005/Atom";
return XName.Get(name, atomNamespace);
}
Upvotes: 6
Reputation: 754725
Try the following
var entryList = element.Elements().Where(x => x.Name.LocalName == "entry");
The problem here is that every element without an explicit namespace is implicitly in the namespace "http://www.w3.org/2005/Atom"
. The LocalName
property can still be used to access the simple name as shown above. Barring that you need to construct a proper XName
element which includes this namespace in order to match the 'entry' nodes
Upvotes: 1