使用 XMLDocument VB.NET 构建 SOAP 消息

发布于 2024-09-10 08:58:31 字数 2170 浏览 4 评论 0原文

我在 VB.NET 中使用 XMLDocument 构建格式正确的 SOAP 消息时遇到一些问题(尽管 C# 答案很好)。

我使用以下代码手动创建我的 SOAP 消息,发生的情况是 soap:Headersoap:Body 的命名空间前缀> 在输出 XML 中被剥离:

Dim soapEnvelope As XmlElement = _xmlRequest.CreateElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
soapEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
soapEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
_xmlRequest.AppendChild(soapEnvelope)
Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", String.Empty)
_xmlRequest.DocumentElement.AppendChild(soapHeader)
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", String.Empty)
_xmlRequest.DocumentElement.AppendChild(soapBody)

这会产生以下输出:

    <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Header>
  ...
 </Header>
 <Body>
  ....
 </Body>
</soap:Envelope>

我需要的是:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soap:Header>
  ...
 </soap:Header>
 <soap:Body>
  ....
 </soap:Body>
</soap:Envelope>

注意: 我很欣赏所有的输入,但无论任何关于 SOAP 的参考应该在接收端工作或解析或类似的东西,底线是我需要生成如上所述的 XML。提前致谢!

解决方案: 我解决这个问题的方法与Quartmeister答案非常相似。这个问题实际上与命名空间有关。不过,我没有每次都使用字符串值,而是使用 DocumentElementNamespaceURI 来使用以下解决方案:

Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", _xmlRequest.DocumentElement.NamespaceURI)
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", _xmlRequest.DocumentElement.NamespaceURI)

I am having some problems building a properly formatted SOAP message using XMLDocument in VB.NET(C# answers are fine though).

I am using the following code to manually create my SOAP message, what is happening is that the namespace prefix of the soap:Header and soap:Body are being stripped in the output XML:

Dim soapEnvelope As XmlElement = _xmlRequest.CreateElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
soapEnvelope.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
soapEnvelope.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
_xmlRequest.AppendChild(soapEnvelope)
Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", String.Empty)
_xmlRequest.DocumentElement.AppendChild(soapHeader)
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", String.Empty)
_xmlRequest.DocumentElement.AppendChild(soapBody)

This results in the following output:

    <?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Header>
  ...
 </Header>
 <Body>
  ....
 </Body>
</soap:Envelope>

What I need is:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soap:Header>
  ...
 </soap:Header>
 <soap:Body>
  ....
 </soap:Body>
</soap:Envelope>

NOTE: I appreciate all input, but regardless of any references to how SOAP should work or be parsed on the receiving side or anything like that, the bottom line is I need to generate the XML as described for. Thanks in advance!

SOLUTION:
Very similar to Quartmeister answer was the way I resolved this. The issue was in fact in relation to namespace. Rather than use the string value every time though, I used the following solution using the NamespaceURI of the DocumentElement:

Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", _xmlRequest.DocumentElement.NamespaceURI)
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", _xmlRequest.DocumentElement.NamespaceURI)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2024-09-17 08:58:32

您需要将 Header 和 Body 元素上的 XML 命名空间设置为soap 命名空间:

Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/")
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")

You need to set the XML namespace on the Header and Body elements to the soap namespace:

Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", "http://schemas.xmlsoap.org/soap/envelope/")
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文