Friday, August 10, 2007

SelectSingleNode not selecting the node.

Recently I was working on creating an ATOM feed reader. Obtained the JS from DynamicDrive and coded the control to take up the URL and return back the posts from it.

It required XML reading and playing around with the nodes. Strange enough, looked easier to work, I had a hard time getting the node required to display the things out.
Below is the format that an ATOM xml uses:


<?xml version='1.0'
encoding='UTF-8'?>

<?xml-stylesheet
href="http://www.blogger.com/styles/atom.css"
type="text/css"?>

<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>

<id>tag:blogger.com,1999:blog-36333526</id>
<updated>2007-08-08T16:18:29.608+05:30</updated>
<title type='text'>Ashutosh Vyas's
Blog</title>

...
...
...
...
<author>
<name>
Ashu
</name>
</author>
<generator version='7.00'
uri='http://www.blogger.com'>
Blogger</generator>
<openSearch:totalResults>22</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>tag:blogger.com,1999:blog-36333526.post-3860689405428340431</id>
<published>2007-08-02T14:47:00.000+05:30</published>
<updated>2007-08-02T14:54:53.163+05:30</updated>
<title type='text'>Asynchronous
Page Concept in ASP.NET</title>

<content type='html'>
<link rel='replies'
type='application/atom+xml'>

<link rel='self'
type='application/atom+xml'

...
...
...
<author>
<name>
Ashu
</name>
</author>
</entry>


Now all I needed was to find out the root node and traverse to the Node "feed/title" to find out the title of the blog to display on the top of the scroller.

To my knowledge, it was as easy as
rssDoc.SelectSingleNode("feed/title").InnerText;
But that did not happen to be the case. It always returned me null.
I tried grabbing out the root node (feed) using
rssDoc.SelectSingleNode("feed/title").InnerText;
but this would again return me the same NULL.
Strange for me, doing a rssDoc.DocumentElement() would most certainly return me the required feed node.
After a bit of help from MSDN and other group, I discovered what I did not knew till now and I suspect many ppl do not because of lack of use.
You require a NAMESPACEMANAGER to get those nodes out.
So to dig out something from

<feed>
<title>
</feed>
</feed>

we need the following code.

XmlNode feedNode = rssDoc.DocumentElement;

XmlNamespaceManager nsMgr = new XmlNamespaceManager(rssDoc.NameTable);

nsMgr.AddNamespace("prefix", http://www.w3.org/2005/Atom);

String feedTitle = feedNode.SelectSingleNode("prefix:title",nsMgr).InnerText;


-- Ashutosh

No comments:

Post a Comment