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

Community
  • 1
  • 1
Jakubee
  • 634
  • 1
  • 9
  • 30

1 Answers1

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();
Charles Mager
  • 25,735
  • 2
  • 35
  • 45