Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Select XML Nodes with XML Namespaces from an XmlDocument?

my code attempts to grab data from the RSS feed of a website. It grabs the nodes fine, but when attempting to grab the data from a node with a colon, it crashes and gives the error "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." The code is shown below:

WebRequest request = WebRequest.Create("http://buypoe.com/external.php?type=RSS2&lastpost=true");
WebResponse response = request.GetResponse();

StringBuilder sb = new StringBuilder("");
System.IO.StreamReader rssStream = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

for (int i = 0; i < 5; i++)
{
   XmlNode rssDetail;
   rssDetail = rssItems.Item(i).SelectSingleNode("dc:creator");

   if (rssDetail != null)
   {
      user = rssDetail.InnerText;
   }
   else
   {
      user = "";
   }
}

I understand that I need to define the namespace, but am unsure how to do this. Help would be appreciated.

like image 749
Sticky Avatar asked Jan 08 '11 08:01

Sticky


People also ask

How do I select a specific node in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

How do I find the XML namespace?

where <name> is the namespace prefix and <"uri"> is the URI that identifies the namespace. After you declare the prefix, you can use it to qualify elements and attributes in an XML document and associate them with the namespace URI.

Can an XML schema define namespaces?

One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.

What is the difference between XmlNode and XmlElement?

Every XmlElement is XmlNode, but not every XmlNode is XmlElement. XmlElement is just one kind of XmlNode. Others are XmlAttribute, XmlText etc. An Element is part of the formal definition of a well-formed XML document, whereas a node is defined as part of the Document Object Model for processing XML documents.


1 Answers

You have to declare the dc namespace prefix using an XmlNamespaceManager before you can use it in XPath expressions:

XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssStream);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(rssDoc.NameTable);
nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
for (int i = 0; i < 5; i++) {
    XmlNode rssDetail = rssItems[i].SelectSingleNode("dc:creator", nsmgr);
    if (rssDetail != null) {
        user = rssDetail.InnerText;
    } else {
        user = "";
    }
}
like image 115
Frédéric Hamidi Avatar answered Sep 21 '22 21:09

Frédéric Hamidi