Link to home
Start Free TrialLog in
Avatar of Epiktet
Epiktet

asked on

Using XPathNavigator to read Atom XML with ASP.Net

I have a XML feed from Youtube that uses the Atom standard, but ASP.Net 2.0 doesn't seem to be able to handle this standard?

The XML feed start as follows:
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'  [...]

I'm using the code posted below, and this works fine as long as I remove the xmlns attribute (xmlns='http://www.w3.org/2005/Atom') in the XML feed, but as I'm getting the feed directly from Youtube I'm not able to edit it myself and need to find a way that ASP.Net 2.0 can read the code "as is".
XPathDocument document = new XPathDocument("http://www.epiktet.no/feeds/youtubefeed.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/feed/entry/id");
 
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;
       
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);
 
string youtubeVideoID = nodesText.Current.Value.ToString();

Open in new window

Avatar of abel
abel
Flag of Netherlands image

Are you sure you are using the correct link? I do not see that xmlns (namespace) specifier there. I believe it SHOULD be there, but it isn't...

To find out whether this is true or not, you can do the following (it should receive the null namespace, which is an empty string):

XPathNavigator nav2 = navigator.SelectSingleNode("/*").NamespaceURI;

I copied your code and ran it. It gives a proper Video ID for me...
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Epiktet
Epiktet

ASKER

Thank you!!

The *[local-name()='feed']/*[local-name() = 'entry']/*[local-name()='id'] suggestion worked like a charm. I should probably investigate more on WHY the atom issue occurs, but right now I'm happy that it's just working.

Thanks again!
Avatar of Epiktet

ASKER

Great help and right to the point!
You're welcome and glad it worked out. Still wonder why your code worked for me the first time around, with the same sources....