Retrieve Specific Nodes When They Contain an xmlns Namespace Parameter

When an XML file contains a xmlns parameter, you can’t use simple SelectSingleNode to retrieve a specific node. Take the following code, for example:

       31  

Now, try to run the following code with the above XML:

<%@ Import Namespace="System.Xml"%><%@ Page Language="C#" Debug="true"%>

Running the above code will generate the following error message:

System.NullReferenceException: Object reference not set to an instance of an object.Line 16:  Response.Write ("Node Name : " + node.Name); 

The solution is to specifiy the namespace for the XPath. The following code solves the problem and retrieves the node:

<%@ Import Namespace="System.Xml"%><%@ Page Language="C#" Debug="true"%>
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: