使用 XMLDocument VB.NET 构建 SOAP 消息 [英] Building SOAP message with XMLDocument VB.NET

查看:106
本文介绍了使用 XMLDocument VB.NET 构建 SOAP 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

结果如下:

    <?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.提前致谢!

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!

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

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)

推荐答案

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

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/")

这篇关于使用 XMLDocument VB.NET 构建 SOAP 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆