1

I can't figure out what namespaces need to be used for following Xml (sample):

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <ResponseCharge xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <ResponseInfo xmlns="http://www.example.net">
            <ResponseDetail>
              <ResponseCode>00</ResponseCode>
              <RequestIdentifier>1029034</RequestIdentifier>
              <RetrievalReferenceNumber>634716660366</RetrievalReferenceNumber>
              <ProcessingTime>00:03:314</ProcessingTime>
            </ResponseDetail>
            <ResponseTransactionDetail>
              <AVSResponseCode />
              <CVVResponseCode />
              <AuthorizationSourceCode />
              <AuthorizationNumber>004454</AuthorizationNumber>
            </ResponseTransactionDetail>
            <EMVResponseData />
          </ResponseInfo>
        </ResponseCharge>
      </s:Body>
    </s:Envelope>

My code looks like this atm:

    XmlDocument doc = new XmlDocument();
            doc.Load(responseXmlFile);
            var nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
            //nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            //nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
            //nsmgr.AddNamespace("", "http://www.example.net");
            // getting NULL here
            XmlNode node = doc.SelectSingleNode("/s:Envelope/s:Body/ResponseCharge/ResponseInfo/ResponseTransactionDetail/AuthorizationNumber", nsmgr); 

I'm following this post: Namespace Manager or XsltContext needed

4
  • No namespace prefix on ResponseInfo in the XPath expression? Commented Dec 14, 2016 at 11:18
  • Getting NULL for my 'XmlNode' query. Commented Dec 14, 2016 at 11:19
  • I remove commented 'ResponseInfo'. Also changed XmlNode to XmlNodeList as in the link I follow. Commented Dec 14, 2016 at 11:24
  • Looks like there is no namespace for element ResponseCharge. I suggest declaring a prefix for 'example.net' and using it in your query for nodes below ResponseCharge Commented Dec 14, 2016 at 11:25

1 Answer 1

1

You need to include the namespace in your query for the elements in the http://www.example.net/ namespace.

var nsmgr = new XmlNamespaceManager(doc.NameTable);

nsmgr.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ex", "http://www.example.net");

XmlNode node = doc.SelectSingleNode(
    "/s:Envelope/s:Body/ResponseCharge/ex:ResponseInfo/ex:ResponseTransactionDetail/ex:AuthorizationNumber", 
    nsmgr); 

I've arbitrarily picked ex as the prefix here. What's defined in the XmlNamespaceManager only relates to your XPath query, the prefixes used in the document aren't relevant.

Alternatively, I'd suggest you use LINQ to XML:

XNamespace ns = "http://www.example.net"

var doc = XDocument.Load(responseXmlFile);

var authNo = (int)doc.Descendants(ns + "AuthorizationNumber").Single();
Sign up to request clarification or add additional context in comments.

2 Comments

This is in 95% correct answer. There is actually no need for 'ex' prefix for 'ResponseCharge'. Wasted almost all morning for this. Thanks so much!
@Jakubee d'oh. That's what I get for not testing it... have fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.