ASP.NET C# Read XML Node

myship
myship used Ask the Experts™
on
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

<v9:Label>
            <v9:Type>OUTBOUND_LABEL</v9:Type>
            <v9:ShippingDocumentDisposition>RETURNED</v9:ShippingDocumentDisposition>
            <v9:Resolution>200</v9:Resolution>
            <v9:CopiesToPrint>2</v9:CopiesToPrint>
            <v9:Parts>
              <v9:DocumentPartSequenceNumber>1</v9:DocumentPartSequenceNumber>
              <v9:Image>i=</v9:Image>
            </v9:Parts>
          </v9:Label>
          <v9:SignatureOption>SERVICE_DEFAULT</v9:SignatureOption>
        </v9:CompletedPackageDetails>
      </v9:CompletedShipmentDetail>
    </v9:ProcessShipmentReply>
  </env:Body>
</soapenv:Envelope>

Open in new window

Comment
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;

Author

Commented:
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.. .

Author

Commented:
just for reference here is the beginning of the document
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
  <env:Body xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <v9:ProcessShipmentReply xmlns:v9="http://fedex.com/ws/ship/v9">
      <v9:HighestSeverity>WARNING</v9:HighestSeverity>
      <v9:Notifications>
        <v9:Severity>WARNING</v9:Severity>

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

Commented:
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", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("env", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("v9", "http://fedex.com/ws/ship/v9");

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

Open in new window

Commented:
i used something like this

        XmlDocuemnt_RateResponse.LoadXml(strResponse);

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


thanks.

Author

Commented:

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