[.net] XPATH Problem

Started by
13 comments, last by zangetsu 17 years, 10 months ago
"/rdf:RDF/channel" would select all channel nodes that are children of the rdf:RDF node. This would be multiple nodes. I guess that because the method is called SelectSingleNode, it can't return mutiple nodes. Try specifying which Channel Node like this:

"/rdf:RDF/channel[1]"

That should select the first node, but MS is weird. According to WC3 the xpath index should be 1 based, but in IE its is 0 based. I don't know off the top of my head if .Net is 0 or 1 based.
Advertisement
"/rdf:RDF/channel[1]" or "/rdf:RDF/channel[0]" do not work either, besides their is only ever 1 channel element.

Why can't I just use "channel" like I can with rss2.0?

I found out whats going on. The XmlNamespaceManager is being retarded. I found this page that explains it.

As I understand it, the namespace manager handles a default namespace, and an empty namespace in an unexpected way. This is interesting since MSDN says "The prefix to associate with the namespace being added. Use String.Empty to add a default namespace."

and the solution applied to your code:

*************************************************************************
xDoc2.Load(txtRead);

XmlNamespaceManager namesp = new XmlNamespaceManager(xDoc2.NameTable);
namesp.AddNamespace("default", "http://purl.org/rss/1.0/");
namesp.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
namesp.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

XmlNode node6 = xDoc2.SelectSingleNode("/rdf:RDF/default:channel");
*************************************************************************
Hoorraaahhh!!!!

Thank you very much for your help. I was very close to using a sledgehammer on my pc!!
If your ever in canterbury, you can buy me a pint. :)

This topic is closed to new replies.

Advertisement