Removing / extracting the soap and body header from the soap message

I have a soap message shown below. I would like to get only the request element and its child nodes.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
               xmlns:xlink="http://www.w3.org/1999/xlink" 
               xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://location.xsd">
  <soap:Header xmlns="http://www.server.net/schema/request.xsd">
  </soap:Header>
  <soap:Body>
   <Request ReqRespVersion="large" Version="1" EchoToken="1.0" xmlns="http://mynamespace.com">
     .....
   </Request>
 </soap:Body>
</soap:Envelope>

I can get it with this code.

Dim myXDocument As XDocument = XDocument.Load(New StringReader(Request))
Dim Xns As XNamespace = XNamespace.Get("http://mynamespace.com")
SoapBody = myXDocument.Descendants(Xns + "Request").First().ToString

But I donโ€™t want to use a specific name, for example โ€œRequestโ€, because I have more than soap messages, and each has a different xml element name. therefore, I need a generic function instead of creating a specific function for each of them.

I followed these suggestions: Extract SOAP body from SOAP message

but with this code below, but it does not work for me. where am I making a mistake or how to extract a part of the body?

Dim myXDocument As XDocument = XDocument.Load(New StringReader(Request))
Dim Xns As XNamespace = XNamespace.Get("soap="http://www.w3.org/2003/05/soap-envelope")
SoapBody = myXDocument.Descendants(Xns + "Body").First().ToString
0
source share
1

, .

XNamespace.Get("soap="http://www.w3.org/2003/05/soap-envelope")

, XNamespace.Get, URI:

XNamespace.Get("http://www.w3.org/2003/05/soap-envelope")

- :

<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>

child (ren) Body, FirstNode :

SoapBody = myXDocument.Descendants(Xns + "Body").First().FirstNode.ToString()

:

<Request ReqRespVersion="large" Version="1" EchoToken="1.0" xmlns="http://mynamespace.com">
</Request>
+1

All Articles