I have a soap message shown below. I would like to get only request element and its child nodes.
You have the wrong namespace in your second example, and you didn't follow the complete example from the linked answer.
XNamespace.Get("soap="http://www.w3.org/2003/05/soap-envelope")
The parameter passed in to XNamespace.Get
should be the URI only:
XNamespace.Get("http://www.w3.org/2003/05/soap-envelope")
Then your second example will return something like:
<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<Request ReqRespVersion="large" Version="1" EchoToken="1.0" xmlns="http://mynamespace.com">
</Request>
</soap:Body>
If you want just the child(ren) elements of the Body
, then you need to add FirstNode
like this:
SoapBody = myXDocument.Descendants(Xns + "Body").First().FirstNode.ToString()
Which will give you this:
<Request ReqRespVersion="large" Version="1" EchoToken="1.0" xmlns="http://mynamespace.com">
</Request>