在微軟中國找到了一個官方的說法------不建議將DataSet直接作為返回值傳送,因為裡面含有大量復雜的schema以及更改等信息,大部分非.NET語言在解析上有困難。建議使用DataSet.WriteXML方法將簡化後的XML版本作為一個WideString回傳。經過試驗,已經在Delphi下輕松通過,Delphi中還需要使用XML Mapper工具事先生成Transfomation(XTR)文件。
Delphi7客戶端代碼
----------------------------------------------------------------------------------------------------------------------------
unit WSTestMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, InvokeRegistry, StdCtrls, Rio, SOAPHTTPClient, Grids, DBGrids,
DB, DBClient, DBTables, Provider, xmldom, Xmlxform,XMLIntf,XMLDoc,SOAPConst;
type
TForm1 = class(TForm)
HTTPRIO1: THTTPRIO;
Button1: TButton;
Memo1: TMemo;
XMLTransformProvider1: TXMLTransformProvider;
ClientDataSet1: TClientDataSet;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses WSTestDefine;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
A:Service1Soap;
B:WideString;
XMLDoc: IXMLDocument;
begin
A := HTTPRIO1 as Service1Soap;
B := A.GetPersonTable;
Memo1.Lines.Add( B );
ClientDataset1.Active := FALSE;
XMLDoc := NewXMLDocument;
XMLDoc.Encoding := SUTF8;
XMLDoc.LoadFromXML(B);
XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument;
ClientDataset1.Active := TRUE;
end;
end.
---------------------------------------------------------------------------------------
.NET WebService代碼
---------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.OracleClient;
using System.IO;
namespace WS0622
{
/// <summary>
/// Service1 的摘要說明。
/// </summary>
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: 該調用是 ASP.NET Web 服務設計器所必需的
InitializeComponent();
}
#region 組件設計器生成的代碼
//Web 服務設計器所必需的
private IContainer components = null;
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服務示例
// HelloWorld() 示例服務返回字符串 Hello World
// 若要生成,請取消注釋下列行,然後保存並生成項目
// 若要測試此 Web 服務,請按 F5 鍵
[Serializable]
public class Person
{
public Person()
{
}
public Person(string name,string gender)
{
this.Name=name;
this.Gender=gender;
}
public string Name="";
public string Gender="";
}
[WebMethod(CacheDuration=60)]
public Person[] GetPersons()
{
Person Alice=new Person("Alice","Female");
Person Bob=new Person("Bob","Male");
Person Chris=new Person("Chris","Female");
Person Dennis=new Person("Dennis","Male");
return new Person[]{Alice,Bob,Chris,Dennis};
}
[WebMethod]
public string GetPersonTable()
{
DataTable table=new DataTable("Person");
table.Columns.Add("Name");
table.Columns.Add("Gender");
table.Rows.Add(new string[2]{"Alice","Female"});
table.Rows.Add(new string[2]{"Bob","Male"});
table.Rows.Add(new string[2]{"Chris","Female"});
table.Rows.Add(new string[2]{"Dennis","Male"});
table.Rows.Add(new string[2]{"Eric","Male"});
DataSet dataset=new DataSet("PersonTable");
dataset.Tables.Add(table);
System.Text.StringBuilder strbuilder=new System.Text.StringBuilder();
StringWriter writer=new StringWriter(strbuilder);