I have an XML/Soap file that looks like this:
<?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>
<SendData xmlns="http://stuff.com/stuff">
<SendDataResult>True</SendDataResult>
</SendData>
</soap:Body>
</soap:Envelope>
I want to extract the SendDataResult value but am having difficulty doing so with the following code and various other methods I’ve tried. It always returns null even though there’s a value in the element.
XElement responseXml = XElement.Load(responseOutputFile);
string data = responseXml.Element("SendDataResult").Value;
What needs to be done to extract the SendDataResult element.
You can use
Descendantsfollowed byFirstorSingle– currently you’re asking the top level element whether it’s got aSendDataResultelement directly beneath it, which it hasn’t. Additionally, you’re not using the right namespace. This should fix it:Alternatively, navigate directly: