2010-07-20 36 views
1

大廈SOAP消息我有一些問題,建設使用的XMLDocument在VB.NET(C#的答案是罰款雖然)格式正確的SOAP消息。用的XMLDocument VB.NET

我使用下面的代碼手動創建我SOAP消息,正在發生的事情是,肥皂的命名空間前綴:頭肥皂:身體在輸出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如上所述。提前致謝!

SOLUTION: 非常相似,Quartmeister的回答是我解決了這個方式。這個問題實際上與名稱空間有關。而不是使用字符串值,但每一次,我用用的namespaceURIDocumentElement以下解決方案:

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

回答

2

您需要設置標題和正文元素肥皂命名空間中的XML命名空間:

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

@Quartmeister - +1感謝您的回覆。我實際上剛剛使用DocumentElement.NamespaceURI解決了問題,這與您的答案基本相同。因爲它是正確的,所以給你答案。謝謝! – jaywon 2010-07-20 22:47:36