从肥皂消息中删除/提取肥皂标题和正文 [英] removing/extracting soap header and body from soap message
问题描述
我有一个如下所示的肥皂消息.我只想获取request元素及其子节点.
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
但是我不想使用请求"之类的特定名称,因为我不仅拥有肥皂消息,而且每个消息都有不同的xml元素名称.因此,我需要一个通用功能,而不是为每个功能创建特定功能.
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.
我遵循了该建议:从SOAP消息中提取SOAP正文
但是下面有这段代码,但对我不起作用.我在哪里做错了,或者怎么在身体部位内抽出东西?
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
推荐答案
在第二个示例中,您使用了错误的名称空间,并且您没有按照链接的答案中的完整示例进行操作.
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")
传递给XNamespace.Get
的参数应仅是URI:
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>
如果只需要Body
的子元素,则需要像这样添加FirstNode
:
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()
哪个会给你这个:
<Request ReqRespVersion="large" Version="1" EchoToken="1.0" xmlns="http://mynamespace.com">
</Request>
这篇关于从肥皂消息中删除/提取肥皂标题和正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!