【问题标题】:Obtain XML section in string VB.NET获取字符串 VB.NET 中的 XML 部分
【发布时间】:2025-12-24 11:20:17
【问题描述】:

我正在使用 vb.net,并且我有一个 WebService,它因此向我发送了一个 SOAP 信封。

<?xml version="1.0" encoding="utf-8"?>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <tfd:Answer DateAnswer="2014-05-02T17:19:38" Certificate="30001000000100000801" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    </soapenv:Body>
</soapenv:Envelope>

我已经能够从 de XML 中获取单个值,例如 DateAnswer 或 Certificate,但现在我需要接收带有“Answer”标签的整个部分以供以后存储。换句话说:

<tfd:Answer DateAnswer="2014-05-02T17:19:38" Certificate="30001000000100000801" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

有没有一种简单的方法来获取字符串中的整个部分?我已经查过了,但我感觉我没有使用正确的关键字进行搜索。

【问题讨论】:

  • 为什么不直接使用“添加服务引用”来访问服务呢?见How to Consume a Web Service。您不应该使用 XML。
  • 但我需要的是使用该网络服务而不是网络服务本身的一部分的结果的整个部分。
  • 这将为您提供整个结果,并且您可以对其中的一部分进行您想要的操作。

标签: xml vb.net web-services soap


【解决方案1】:

这是我用来解决问题的代码。

Dim Xns As XNamespace = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/")
mySection = myXMLDocument.Descendants(Xns + "Body").First().FirstNode.ToString()

“mySection”包含我需要的字符串。

【讨论】: