tags:

views:

81

answers:

3

I'm trying to get channel element from this document.

<rdf:RDF 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns="http://purl.org/rss/1.0/" 
xmlns:slash="http://purl.org/rss/1.0/modules/slash/" 
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" 
xmlns:admin="http://webns.net/mvcb/" 
xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"&gt;

    <channel rdf:about="http://developers.slashdot.org/"&gt;
     <title>Slashdot: Developers</title>
     <link>http://developers.slashdot.org/&lt;/link&gt;
     ...

I think it's in the default namespace, which seems to be "http://purl.org/rss/1.0/" so I tried like this:

XmlNamespaceManager nsmsgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmsgr.AddNamespace(String.Empty, "http://purl.org/rss/1.0/");

XmlNode root = rssDoc.DocumentElement;
XmlNode channel = rssDoc.SelectSingleNode("channel", nsmsgr);

I doesn't work. XmlNode channel stays null.

A: 

Just do:

XmlNode channel = rssDoc.SelectSingleNode(@"//channel");
Nix
What about the default XML namespace: `xmlns="http://purl.org/rss/1.0/" ` ??
marc_s
its not qualified.
Nix
But you still need to **define** it, I believe... qualified or not...
marc_s
@marc_s: It's the "rdf" prefix that's optional. See my answer.
Steven Sudit
A: 
XmlElement root = rssDoc.DocumentElement;

XmlNode channel = root.SelectSingleNode("/channel");

That will get you the channel node, you can then reference, Attributes, Value, InnerXML, FirstChild etc to pull the data from that node.

*edit: should have been XmlElement instead of Node

jon3laze
That doesn't actually work.
Steven Sudit
+2  A: 

You can't add it as Empty.

http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.addnamespace.aspx

The prefix to associate with the namespace being added. Use String.Empty to add a default namespace. Note If the XmlNamespaceManager will be used for resolving namespaces in an XML Path Language (XPath) expression, a prefix must be specified. If an XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. For more information about XPath expressions and the XmlNamespaceManager, refer to the XmlNode.SelectNodes and XPathExpression.SetContext methods.XPathExpression.SetContext methods.

So just add the default prefix as "default", then use "/*/default:channel".

Working code:

        var nsmsgr = new XmlNamespaceManager(rssDoc.NameTable);
        nsmsgr.AddNamespace("default", "http://purl.org/rss/1.0/");

        var root = rssDoc.DocumentElement;
        var channel = rssDoc.SelectSingleNode("/*/default:channel", nsmsgr);

The above code works, but it's got a hardcoded URI and it uses a "cheat" to avoid dealing with the root node. Here's a cleaner, more general solution:

        var nsmsgr = new XmlNamespaceManager(rssDoc.NameTable);
        var root = rssDoc.DocumentElement;
        nsmsgr.AddNamespace("default", root.GetAttribute("xmlns"));
        nsmsgr.AddNamespace("rdf", root.GetAttribute("xmlns:rdf"));
        var channel = rssDoc.SelectSingleNode("/rdf:RDF/default:channel", nsmsgr);
Steven Sudit
This works, thanks!
bruno