Reputation: 51
I attempt to create a xml document to access a webservice. I'm having trouble getting the result xml beeing like i want it to. :-) This is my delphicode to create this document.
xmlQuery.Active := true;
xmlQuery.Version := '1.0';
xmlQuery.Encoding := 'UTF-8';
lEnvelope := xmlQuery.AddChild('soap:Envelope');
lEnvelope.Attributes['xmlns:soap'] := 'http://schemas.xmlsoap.org/soap/envelope/';
lHeader := lEnvelope.AddChild('soap:Header');
lBruker := lHeader.AddChild('brukersesjon:Brukersesjon');
lValue := lBruker.AddChild('distribusjonskanal');
lValue.Text := 'PTP';
lValue := lBruker.AddChild('systemnavn');
lValue.Text := 'infotorgEG';
The resulting xml looks like this.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<brukersesjon:Brukersesjon>
<brukersesjon:distribusjonskanal>PTP</brukersesjon:distribusjonskanal>
<brukersesjon:systemnavn>infotorgEG</brukersesjon:systemnavn>
</brukersesjon:Brukersesjon>
</soap:Header>
</soap:Envelope>
I want it to look like this.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<brukersesjon:Brukersesjon>
<distribusjonskanal>PTP</distribusjonskanal>
<systemnavn>infotorgEG</systemnavn>
</brukersesjon:Brukersesjon>
</soap:Header>
</soap:Envelope>
I can't figure out what i'm doing wrong. Can anyone of you help me? Does anyone of you know of a sample/tutorial that creates a xml file complete with header and body?
Upvotes: 2
Views: 181
Reputation: 125749
Use the overloaded IXMLNode.AddChild
that takes a second parameter for the NameSpaceURI
:
xmlQuery.Active := true;
xmlQuery.Version := '1.0';
xmlQuery.Encoding := 'UTF-8';
lEnvelope := xmlQuery.AddChild('soap:Envelope');
lEnvelope.Attributes['xmlns:soap'] := 'http://schemas.xmlsoap.org/soap/envelope/';
lHeader := lEnvelope.AddChild('soap:Header');
lBruker := lHeader.AddChild('brukersesjon:Brukersesjon');
lValue := lBruker.AddChild('distribusjonskanal', ''); // <<<-- Here
lValue.Text := 'PTP';
lValue := lBruker.AddChild('systemnavn', ''); // <<<-- And here
lValue.Text := 'infotorgEG';
This produces the output you show as the desired output.
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header><brukersesjon:Brukersesjon>
<distribusjonskanal>PTP</distribusjonskanal>
<systemnavn>infotorgEG</systemnavn>
</brukersesjon:Brukersesjon>
</soap:Header>
</soap:Envelope>
Upvotes: 1