vb.net 从肥皂消息中删除/提取肥皂头和正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20270314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:58:51  来源:igfitidea点击:

removing/extracting soap header and body from soap message

xmlvb.netlinq-to-xml

提问by batmaci

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.

但我不想使用像“请求”这样的特定名称,因为我有更多的肥皂消息,而且每个消息都有不同的 xml 元素名称。所以我需要一个通用函数,而不是为每个函数创建一个特定的函数。

I followed that suggestion(s): Extract SOAP body from a SOAP message

我遵循了该建议:从 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

回答by Tim

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.Getshould be the URI only:

传入的参数XNamespace.Get应该只是 URI:

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 FirstNodelike this:

如果你只想要 的 child(ren) 元素Body,那么你需要FirstNode像这样添加:

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>