2011-01-12 55 views
1

我一直有這個問題很長一段時間了,我無法解決它自己。我試過Google,Bing和stackOverflow?沒有運氣...如何使用TXMLDocument手動構建肥皂信封(Delphi 2006)

我試圖手動構建一個SOAP頭使用Delphi 2006中的TXMLDocument的組件:

... ... ... ... ... ...

我在做什麼是我要構建一個所謂的新元素「肥皂:信封」。在這個新元素中,我創建了三個名爲「xmlns:soap」,「xmlns:xsd」和「xmlns:xsi」的屬性。

當我試圖寫在任何三個屬性,然後我得到下面的錯誤值:

試圖修改只讀節點。

有沒有人知道如何使用TXMLDocument來完成這項任務?

/布賴恩

+0

<皁提供的代碼:信封的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「> ... ... ... ... ... ...

回答

2

下面的代碼在這裏工作正常:

procedure WriteSoapFile; 
var 
    Document: IXMLDocument; 
    Envelope: IXMLNode; 
    Body: IXMLNode; 
begin 
    Document := NewXMLDocument; 
    Envelope := Document.AddChild('soap:Envelope'); 
    Envelope.Attributes['xmlns:soap'] := 'schemas.xmlsoap.org/soap/envelope/'; 
    Envelope.Attributes['xmlns:xsd'] := 'w3.org/2001/XMLSchema'; 
    Envelope.Attributes['xmlns:xsi'] := 'w3.org/2001/XMLSchema-instance'; 
    Body := Envelope.AddChild('soap:Body'); 
    Document.SaveToFile('Test.xml'); 
end; 

你應該能夠使用TXMLDocument而不是IXMLDocument,它僅僅是個接口周圍部件的包裝材料。

+0

哇。這樣可行。非常感謝!!!我試圖使用屬性NodeValue設置值:= ....; 我正在使用以下語義(非常簡單):MyNode:= Document.CreateNode(....); MyNode.NodeValue:= ....;/Brian –

2

這是我的解決方案,它使用DeclareNamespace聲明命名空間:

procedure WriteSoapFile; 
const 
    NS_SOAP = 'schemas.xmlsoap.org/soap/envelope/'; 
var 
    Document: IXMLDocument; 
    Envelope: IXMLNode; 
    Body: IXMLNode; 
begin 
    Document := NewXMLDocument; 
    Envelope := Document.CreateElement('soap:Envelope', NS_SOAP); 
    Envelope.DeclareNamespace('soap', NS_SOAP); 
    Envelope.DeclareNamespace('xsd', 'w3.org/2001/XMLSchema'); 
    Envelope.DeclareNamespace('xsi', 'w3.org/2001/XMLSchema-instance'); 
    Body := Envelope.AddChild('Body'); 
    Document.DocumentElement := Envelope; 
    Document.SaveToFile('Test.xml'); 
end; 

基於在How to set the prefix of a document element in Delphi

+0

也感謝這個解決方案。我愛你們(o;謝謝你,謝謝你,謝謝!!! –

+0

由於問題只是關於頭部,我沒有打擾設置命名空間,但你的解決方案當然是更清潔。 –