我在构建一个格式正确的SOAP消息时遇到了一些问题,该消息使用XMLDocument in VB.NET(C#答案很好)。
我使用以下代码手动创建SOAP消息,所发生的情况是soap:Header和soap: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应该如何工作或在接收端被解析的引用,或者诸如此类的东西,我都需要像描述的那样生成。提前感谢!
Quartmeister解决方案:与答案非常相似的是我解决这个问题的方式。这一问题实际上与名称空间有关。不过,与其每次使用字符串值,不如使用下面的解决方案,使用NamespaceURI的DocumentElement
Dim soapHeader As XmlElement = _xmlRequest.CreateElement("soap", "Header", _xmlRequest.DocumentElement.NamespaceURI)
Dim soapBody As XmlElement = _xmlRequest.CreateElement("soap", "Body", _xmlRequest.DocumentElement.NamespaceURI)发布于 2010-07-20 21:28:52
您需要将头元素和正文元素上的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/")https://stackoverflow.com/questions/3294251
复制相似问题