# [.net] XPATH Problem

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
SelectSingleNode will return Null when the xpath does not resolve to a valid node. So to get the title you would want to do something like this:

nodeTitle = xDoc2.DocumentElement.SelectSingleNode("\rdf\channel\title");
strTitle = nodeTitle.InnerText;

chances are you are going to want to select different nodes in your reader, so you'll want to create more detailed xpaths. There is a good tutorial on xpaths on w3schools.com

##### Share on other sites
The problem being that the rdf part is unique to this particular feed (and other feeds may also have namespaces). I will not know what to put in the xpath. is there a way to find this prefix in code?

##### Share on other sites
Since you are using the Xml.XmlDocument it has a member that is a collection of child nodes, and the DocuementElement. You can do something like this (forgive the psuedo vb.net syntax) To determine what sort of feed it is and react to that.
Select Case xDoc2.DocumentElement.LocalName.ToLower    Case "rdf"        LoadRdfFeed(xDoc2)    case "rss"        LoadRssFeed(xDoc2)    Case "atom"        LoadAtomFeed(xDoc2)End Select'Additionally, can look through the ChildNodes collection.For Each cNode in XDoc2.DocumentElement.ChildNodes    If cNode.LocalName.ToLower = "channel" then        LoadChannelNode(cNode)    end ifNext

##### Share on other sites
Due to the last post I have realised that the problematic feed is rdf not rss2.0. This explains the fact that the rdf file has items on the same level as the channel instead of as siblings of the channel.

I am still having problems with the namespace though.

I now have the following code:-

*************************************************************************

XmlNamespaceManager namesp = new XmlNamespaceManager(xDoc2.NameTable);

XmlNodeList node1 = xDoc2.GetElementsByTagName("channel");
XmlNode node2 = xDoc2.DocumentElement.SelectSingleNode("rdf:RDF",namesp);
XmlNode node3 = xDoc2.DocumentElement.SelectSingleNode("rdf:channel",namesp);
XmlNode node4 = xDoc2.DocumentElement.SelectSingleNode("channel",namesp);
XmlNode node5 = xDoc2.DocumentElement.SelectSingleNode("channel");
XmlNode node6 = xDoc2.DocumentElement.SelectSingleNode("RDF/channel");

**************************************************************************

the only node that is not null is node1.

What am I doing wrong?

##### Share on other sites
GetElementsByTagName expects a plain string name.
SelectSingleNode expects a valid xpath. An xpath always starts with a /, and none of your xpaths start with the slash. Try something like this:

XmlNode node6 = xDoc2.DocumentElement.SelectSingleNode("/RDF/channel");

xpaths can be a hard concept to get your head around for the first time. I strongly recommend reading through the tutorial I linked earlier.

edit:
correction, not all paths start with /, but I make it a habit to do so since it is less confusing to me to use absolute paths, and you are less like to have problems than when dealing with relative paths.

##### Share on other sites
If I write

XmlNode node6 = xDoc2.DocumentElement.SelectSingleNode("/RDF/channel");

it still does not work.

Also if I use the following statement with the rss feed that does work, node6 is null

XmlNode node6 = xDoc2.DocumentElement.SelectSingleNode("/channel");

but the following statement works fine.

XmlNode node6 = xDoc2.DocumentElement.SelectSingleNode("channel");

it seems that prefixing a "/" breaks everything.

##### Share on other sites
Try SelectSingleNode("//channel") -- the double slash means "any node named ... recursively".

There's a decent XPath tutorial On W3Schools.com, which you can also use as a reference.

##### Share on other sites
ok lets see if I can be a little more clear.
A xpath that starts with / is an absolute path so it starts with the root element.
In this case the root node/element is:

I think the local name of this elememt is "rdf:RDF" you should be able to confirm this by evaluating xDoc2.DocumentElement.LocalName

If this is the case then the first node in an absolute xpath would be "/rdf:RDF" knowing that then the xpath "/channel" would suggest that the root element is a channel node. we know this is not the case because the root node is the rdf:RDF node. So of course "/channel" would not work. However channel is a child of rdf:RDF. The reason why xDoc2.DocumentElement.SelectSingleNode("channel"); works is because this xpath "channel" is a relative path becauswe it does not have the leading /. Its relative to the DocumentElement (which is the same as the root node.) Theis xpath is evaluated as "\rdf:RDF\channel" which is valid.

these two statements should select the same node.
xDoc2.DocumentElement.SelectSingleNode("channel")
xDoc2.DocumentElement.SelectSingleNode("\rdf:RDF\channel")

The first selects the child node of DocumentElement named "channel". The second finds the rdf:RDF root node(same as DocumentElement), then finds the Channel node under that.

Just incase we aren't clear, DocumentElement is always the Root Element, and conceptually Element and Node mean the same thing.

##### Share on other sites
Ok, I am partially there now.

xDoc2.DocumentElement.SelectSingleNode("/rdf:RDF") returns a node

xDoc2.DocumentElement.SelectSingleNode("/rdf:RDF/channel") returns null

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 15
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632602
• Total Posts
3007363

×