devxlogo

Retrieve Specific Nodes When They Contain an xmlns Namespace Parameter

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"%>
See also  How HealthStream Learning Center Supports Healthcare Education and Compliance
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist