ℛⱥℐℰşℎ
ℛⱥℐℰşℎ

Reputation: 360

Read element from SOAP message

I'm trying to read the APP_DATE element in this xml message. Tried using LINQ but unable to read MyResult

<?xml version="1.0" encoding="utf-8" ?> 
 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
 <MyResponse xmlns="http://tempuri.org/">
 <MyResult>
  <APP_TYPE>I</APP_TYPE> 
  <APP_NO>152240</APP_NO> 
  <APP_DATE>10/03/2016</APP_DATE> 
  </MyResult>
  </MyResponse>
  </soap:Body>
 </soap:Envelope>

Upvotes: 1

Views: 51

Answers (1)

Gilad Green
Gilad Green

Reputation: 37299

Can't be sure because you didn't supply any example code but I assume you struggle because of the namespaces. So try this:

XmlDocument doc = new XmlDocument();
doc.Load("data.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
ns.AddNamespace("x", "http://tempuri.org/");

var result = doc.SelectSingleNode("//soap:Envelope/soap:Body/x:MyResponse/x:MyResult/x:APP_DATE", ns).InnerText;

For deeper understanding you can read this question

Using Linq it will be like this:

var result = XDocument.Load("data.xml").Root
                      .Descendants(XName.Get("APP_DATE", "http://tempuri.org/"))
                      .FirstOrDefault()?.Value;

See how in both examples I have to specify the namespaces of what I'm looking for

Upvotes: 1

Related Questions