ASP.NET C# Read XML Node

myship used Ask the Experts™
below is the ending of an xml document

i need to be able to put in a node and read the value of one element in that bix xml document

i am using this like to grab the xmlnode

XmlNode XmlNode_Label = XmlDocument_Response.SelectSingleNode("soapenv:Envelope/env:Body");

I am getting this error
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

so how do i grab the value of the Image element

thanks in advance


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if in the xml file, you could add more information to the nodes:
say instead of having :<v9:Image>i=</v9:Image>, you could have some unique id bound with the element, say:
<v9:Image id = "9">i=</v9:Image>

then you can use Linq to find the node very easily,

XDocument x = XDocument.Load(path);
XElement ele = x.Descendants().Where(e => e.Attribute("ID").Value == 9).Single<XElement>();//find the image node

you have to using these namespaces:

using System.Linq;
using System.Xml.Linq;


no i get the xml request from a webservice
i cannot change the value of anything

well i can.. like parse the thing and do changes but i am sure there is a much easier way.. .


just for reference here is the beginning of the document
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="">
  <env:Header xmlns:env="" xmlns:xsi=""/>
  <env:Body xmlns:env="" xmlns:xsi="">
    <v9:ProcessShipmentReply xmlns:v9="">

Open in new window

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Carl TawnSenior Systems and Integration Developer

The issue you are running into is down to the namespaces. In this scenario you need to use a XmlNamespaceManager to provide context information about the namespaces in the document, plus a prefix to access them:
XmlDocument doc = new XmlDocument();
doc.Load("Your xml");

XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("soap", "");
ns.AddNamespace("env", "");
ns.AddNamespace("v9", "");

XmlNode imageNode = doc.SelectSingleNode("//v9:Label", ns);

Open in new window

i used something like this


        XmlNode XmlNode = xmlDocumentResponse.GetElementsByTagName("v9:Image")[0];




Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial