2

我尝试创建一个 xml 文档来访问 Web 服务。我无法像我想要的那样获得结果 xml 蜜蜂。:-) 这是我创建此文档的 delphicode。

  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';

生成的 xml 如下所示。

<?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>

我希望它看起来像这样。

<?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>

我无法弄清楚我做错了什么。你们中的任何人都可以帮助我吗?你们中有人知道创建包含标题和正文的 xml 文件的示例/教程吗?

4

1 回答 1

1

IXMLNode.AddChild使用带有第二个参数的重载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';

这会产生您显示为所需输出的输出。

<?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>
于 2014-12-23T19:54:34.227 回答