1

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>
Gilad Green
  • 36,708
  • 7
  • 61
  • 95

1 Answers1

1

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

Community
  • 1
  • 1
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • What does "?" do in your linq querry? – Git Aug 17 '16 at 09:18
  • 1
    @gismo - it is c# 6.0's syntactic sugar for `x == null ? /*default of type of x*/ : x.Value`. You can ready about it in [MSDN](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx) – Gilad Green Aug 17 '16 at 09:21