I've been having this problem for a long time now and I cannot solve it my se开发者_如何学运维lf. I've tried searching Google, Bing and stackOverflow too? No luck...
I'm trying to construct a soap header manually using the TXMLDocument component of Delphi 2006:
... ... ... ... ... ...
What I'm doing is that I'm constructing a new Element called 'soap:Envelope'. In this new element I'm creating three attribtues called: 'xmlns:soap', 'xmlns:xsd' and 'xmlns:xsi'.
When I'm trying to write a value in any of the three attributes then I'm getting the error below:
Attempt to modify a read-only node.
Does any one know how to do this task using the TXMLDocument?
/Brian
The following code works fine here:
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;
You should be able to use TXMLDocument
instead of IXMLDocument
, it is just a component wrapper around the interface.
Here is my solution, it uses DeclareNamespace to declare namespaces:
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;
Based on the code provided in How to set the prefix of a document element in Delphi
精彩评论