Reputation: 6893
I have a soap message shown below. I would like to get only 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 am able to get it using 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 dont want to use specific name like "Request" because I have more than soap messages and each have different xml element name. so I need a general function instead of making a specific function for each.
I followed that suggestion(s): Extract SOAP body from a SOAP message
but with this code below, but it doesnt work for me. where am I doing the mistake or how do I extract inside the body part?
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
Upvotes: 0
Views: 16679
Reputation: 28530
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>
Upvotes: 2