Groups | Blog | Home
all groups > dotnet xml > december 2003 >

dotnet xml : Highly annoying namespace problem



Ayende Rahien
12/5/2003 7:36:50 PM
I've the following code, taken from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml02172003.asp
to select a node from RSS file.
However, I'm always getting a null returning for the SelectSingleNode(),
when trying to load a RSS 1.0 document, (which has a default namespace)
why?

if(feed.DocumentElement.LocalName.Equals("RDF") &&
feed.DocumentElement.NamespaceURI.Equals("http://www.w3.org/1999/02/22-rdf-s
yntax-ns#"))

{

NamespaceUri = "http://purl.org/rss/1.0/";

}

else if(feed.DocumentElement.LocalName.Equals("rss"))

{

NamespaceUri = feed.DocumentElement.NamespaceURI;

}

else

{

throw new RssFeedException(ResourceFetcher.GetException("Illegal feed"));

}

XmlNamespaceManager nsMgr = new XmlNamespaceManager(feed.NameTable);

nsMgr.AddNamespace("rss",NamespaceUri);

node = feed.SelectSingleNode("//rss:channel/title",nsMgr);//Select feed's
title
//node = feed.SelectSingleNode("//rss:channel/rss:title",nsMgr);//Doesn't
work as well!!!


if(node!=null)//It's ALWAYS null if there is a default namespace!

title = node.InnerText;




Oleg Tkachenko
12/7/2003 2:33:13 PM
[quoted text, click to view]

Which document are you trying to load?
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
ru
12/14/2003 1:42:22 PM
I know your frustration, as I myself struggled with this
for weeks and weeks...
Here's how you do it:
dim rss as New XmlTextReader(URL)
While rss.Read()
If rss.NodeType = XmlNodeType.Element Then
Select Case rss.Name.ToLower()
Case "item"
strItem = rss.ReadString()
Case "pubdate"
strDate = rss.ReadString()
Case "dc:date"
strDate = rss.ReadString()
etc.
end While


[quoted text, click to view]
AddThis Social Bookmark Button