0

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
Community
  • 1
  • 1
Emil
  • 6,411
  • 7
  • 62
  • 112
  • 1
    Is there a reason you are doing this by hand? A reason why you haven't just written a WCF service or client? – John Saunders Nov 29 '13 at 00:40
  • @john: indeed my service is wcf and I had published operations taking object and returning object. My client told me that he wants to send string and this string has soap envelope around including header and body as my example. For me in order to deserilaze it into object again, I need to extract request part only. Indeed, I am new in wcf but I thought wcf adds soap envelope automatically, doesn't? When I check with fiddler, I can see the request is already as in my example, I dont know if I should tell it to my client that they dont need to send soap tags. Do I understand wcf correctly? – Emil Nov 29 '13 at 11:15
  • 2
    Clearly, you and your client are not communicating. Yes, of course you should tell your client! – John Saunders Nov 29 '13 at 11:26

1 Answers1

2

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>
Tim
  • 28,212
  • 8
  • 63
  • 76
  • thanks for corecting me about namespace mistake but one thing I can't find out that firstnode() is not available for me in vb.net. Is it only c#? – Emil Nov 29 '13 at 11:22
  • @batmaci - try `FirstNode` (without the `()`). `FirstNode` is a property, not a method. – Tim Nov 29 '13 at 18:15