Delphi soap响应是否保存为xml?

Delphi soap响应是否保存为xml?,delphi,soap-client,Delphi,Soap Client,如何将soap响应保存为xml?我尝试使用tstringlist、filestream创建xml文件,但是,我得到了 无法将类型(null)的varinat转换为类型(olestr) 我尝试了这个简单的代码。响应不是空的。文件大小为40MB。Delphi XE3 procedure TForm1.HTTPRIO1AfterExecute(const MethodName: string; SOAPResponse: TStream); var xml : TStringlist; begin

如何将soap响应保存为xml?我尝试使用tstringlist、filestream创建xml文件,但是,我得到了

无法将类型(null)的varinat转换为类型(olestr)

我尝试了这个简单的代码。响应不是空的。文件大小为40MB。Delphi XE3

procedure TForm1.HTTPRIO1AfterExecute(const MethodName: string;
SOAPResponse: TStream);
var xml : TStringlist;
begin
 xml := TStringlist.create;
  try
    soapresponse.Position:=0;
    xml.LoadFromStream(SOAPResponse);
    xml.SaveToFile('...file.xml');
 finally
  xml.Free;
 end;
end;
这就是问题所在(空行)?是否

...

- <leiras>
- <![CDATA[ 
*  Socket AM2 for AMD Athlon™ 64FX / 64X2 / 64 and Sempron processors
* ULi M1697

1. Supports FSB 1000MHz (2.0GT/s), Hyper-Transport Technology and AMD Cool 'n' Quiet   Technology
2. Untied Overclocking : During Overclocking, FSB enjoys better margin due to fixed PCIE/ PCI Buses
3. Supports Dual Channel DDRII800/667/533, 4 x DIMM slots, with maximum capacity up to 8GB
4. Hybrid Booster - ASRock Safe Overclocking Technology
5. Supports Dual Graphics XLI
6. 1 x PCI Express x16 slot
7. 1 x PCI Express x 8 slot, to adopt 2nd PCI Express x 16 VGA card and other PCI Express x4, x2, x1 interface cards
8. 2 x PCI Express x1 slots
9. 4 x Serial ATA II 3.0Gb/s, support RAID (RAID 0, 1, 0+1, JBOD and RAID 5), NCQ, AHCI and "Hot Plug" functions
10. 2 x eSATAII 3.0Gb/s, support NCQ, AHCI and "Hot Plug" functions
11. HDMI_SPDIF header, providing SPDIF audio output to HDMI VGA card, allows the system to connect HDMI Digital TV/projector/LCD devices.
12. 7.1 Channel with High Definition Audio
13. Windows Vista™ Premium Logo Hardware Ready
14. ASRock 8CH_eSATAII I/O: 2 eSATAII ports, HD 7.1 channel audio jacks


]]> 
</leiras>
<kepek /> 

...

尝试使用流的读取方法来读取临时字符串变量:

procedure TForm1.HTTPRIO1AfterExecute(const MethodName: string; SOAPResponse: TStream);
var
  s: string[200];
  ds: string;
  n: Longint;
  xml: TStringList;
begin
  SOAPResponse.Position := 0;
  xml := TStringList.Create;
  try
    ds := EmptyStr;
    while SOAPResponse.Position < SOAPResponse.Size do begin
      n := SOAPResponse.Read(s[1], 200);
      SetLength(s, n);
      xml.Text := xml.Text + ds;
    end;

    xml.SaveToFile('...file.xml');
  finally
    xml.Free;
  end;
end;
procedure TForm1.HTTPRIO1AfterExecute(const-MethodName:string;SOAPResponse:TStream);
变量
s:字符串[200];
ds:字符串;
n:朗吉特;
xml:TStringList;
开始
SOAPResponse.位置:=0;
xml:=TStringList.Create;
尝试
ds:=EmptyStr;
当SOAPResponse.Position
您是否尝试将流直接转储到文件中,以查看其中的内容

procedure TForm1.HTTPRIO1AfterExecute(const MethodName: string; SOAPResponse: TStream);
var 
  FS: TFileStream;
begin
  FS := TFileStream.Create('C:\Downloaded.xml', fmCreate);
  try
    FS.CopyFrom(SoapResponse, 0);
  finally
    FS.Free;
  end;
end;
奇怪。
也许这个例子有帮助。
我正在做几乎相同的事情,将响应和请求记录到TMemo中。
此外,我的“Before”例程从外部FSoapData TStringStream填充实际的SOAPRequest:

procedure TFrmTestEWS.HTTPRIOBeforeExecute(const MethodName: string; SOAPRequest: TStream);
var
   TS: TStringStream;
   S : String;
begin
   // 1. Fill request
   SOAPRequest.Position := 0;
   FSoapData.Position := 0;
   SOAPRequest.CopyFrom(FSoapData,FSoapData.Size);
   SOAPRequest.Size := FSoapData.Size;
   // 2. Logging
   S := 'Request:' + #13#10#13#10;
   TS := TStringStream.Create(S);
   try
      TS.Position := Length(S);
      FSoapData.Position := 0;
      TS.CopyFrom(FSoapData,FSoapData.Size);
      TS.Position := 0;
      MmoLog.Lines.LoadFromStream(TS);
   finally
      TS.Free;
   end;
end;

procedure TFrmTestEWS.HTTPRIOAfterExecute(const MethodName: string; SOAPResponse: TStream);
var
   TS: TStringStream;
   S : String;
begin
   S := MmoLog.Text + #13#10#13#10 + 'Response:' + #13#10#13#10;
   TS := TStringStream.Create(S);
   try
      TS.Position := Length(S);
      SOAPResponse.Position := 0;
      TS.CopyFrom(SOAPResponse,SOAPResponse.Size);
      TS.Position := 0;
      MmoLog.Lines.LoadFromStream(TS);
   finally
      SOAPResponse.Position := 0;
      TS.Free;
   end;
end;

@Lacika您的delphi版本很重要,断点是您最好的朋友。这对我来说是正确的,我们会做一些非常类似的事情来记录我们的响应。是时候加入一些调试语句,可能是前255个字符上的ShowMessage之类的东西了。这里显示的代码不涉及任何变体。您的问题在别处。请使用debugger。通过使用TStringList,您可能会在保存的soap响应中应用不需要的ANSI Unicode转换。如果您希望保留接收方式,请不要使用TStream,而是直接将流保存到磁盘,例如,使用TFileStream。尝试将xml用作
TFileStream
并使用
xml.CopyFrom(SOAPResponse,SOAPResponse.Size)
谢谢。我明天会试试。我支持@kobik的建议,
TFileStream
更易于阅读。[+1]@Lacika:此代码是否触发了
无法将类型(null)的变体转换为(olestr)类型
?!?在哪一行发生异常?我是阿马图尔,我的英语不是很好:)@Lacika:谢谢!但是你应该单击[中断]而不是单击[继续],然后告诉我们您在哪一行。这是真正发生错误的地方。同时,我建议您删除此问题,并使用该代码创建一个新问题。谢谢。我明天会尝试。
procedure TFrmTestEWS.HTTPRIOBeforeExecute(const MethodName: string; SOAPRequest: TStream);
var
   TS: TStringStream;
   S : String;
begin
   // 1. Fill request
   SOAPRequest.Position := 0;
   FSoapData.Position := 0;
   SOAPRequest.CopyFrom(FSoapData,FSoapData.Size);
   SOAPRequest.Size := FSoapData.Size;
   // 2. Logging
   S := 'Request:' + #13#10#13#10;
   TS := TStringStream.Create(S);
   try
      TS.Position := Length(S);
      FSoapData.Position := 0;
      TS.CopyFrom(FSoapData,FSoapData.Size);
      TS.Position := 0;
      MmoLog.Lines.LoadFromStream(TS);
   finally
      TS.Free;
   end;
end;

procedure TFrmTestEWS.HTTPRIOAfterExecute(const MethodName: string; SOAPResponse: TStream);
var
   TS: TStringStream;
   S : String;
begin
   S := MmoLog.Text + #13#10#13#10 + 'Response:' + #13#10#13#10;
   TS := TStringStream.Create(S);
   try
      TS.Position := Length(S);
      SOAPResponse.Position := 0;
      TS.CopyFrom(SOAPResponse,SOAPResponse.Size);
      TS.Position := 0;
      MmoLog.Lines.LoadFromStream(TS);
   finally
      SOAPResponse.Position := 0;
      TS.Free;
   end;
end;