Reputation: 698
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.
Upvotes: 4
Views: 1001
Reputation: 1500873
You can use Descendants
followed by First
or Single
- currently you're asking the top level element whether it's got a SendDataResult
element directly beneath it, which it hasn't. Additionally, you're not using the right namespace. This should fix it:
XNamespace stuff = "http://stuff.com/stuff";
string data = responseXml.Descendants(stuff + "SendDataResult")
.Single()
.Value;
Alternatively, navigate directly:
XNamespace stuff = "http://stuff.com/stuff";
XNamespace soap = "http://www.w3.org/2003/05/soap-envelope";
string data = responseXml.Element(soap + "Body")
.Element(stuff + "SendDataResult")
.Value;
Upvotes: 5