Use the DataSet type returned by the web service developed by .NET in Delphi

xiaoxiao2021-03-06  44

In Microsoft China, I found an official statement ------ It is not recommended to transfer DataSet as a return value, because there is a large amount of complex SCHEMA and changes, most non-.NET languages ​​are difficult. It is recommended to use the DataSet.WriteXML method to simplify the XML version as a WideString back. After testing, it has been easily passed under Delphi. Delphi needs to be used to use the XML Mapper tool to make a transfomation (XTR) file.

Delphi7 client code

-------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------

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

XmlTransformRovider1.transformRead.sourceXmldocument: = xmldoc.getDomdocument;

ClientDataSet1.Active: = TRUE;

END;

End.

-------------------------------------------------- -------------------------------------

.NET WebService code

-------------------------------------------------- -------------------------------------

Using system;

Using system.collections;

Using system.componentmodel; using system.data;

Using system.diagnostics;

Using system.Web;

Using system.Web.services;

Using system.data.oraclient;

Using system.io;

Namespace WS0622

{

///

/// service1 summary description. ///

Public class service1: system.web.services.Webservice

{

Public service1 ()

{

// Codegen: This call is necessary for the ASP.NET Web service designer.

InitializationComponent ();

}

#REGION component designer generated code

/ / Web service designer necessary

Private icontainer Components = NULL;

///

/// Designer supports the required method - do not use the code editor to modify the // / this method. ///

Private vidinitiRizeComponent ()

{

}

///

/// Clean all the resources being used. ///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing && Components! = NULL)

{

Components.dispose ();

}

Base.dispose (Disposing);

}

#ndregion

// Web Service Sample // HelloWorld () Sample Service Returns Hello World // To generate, cancel the following list, then save and generate items // To test this web service, press F5 button

[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 persons [] 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);

Dataset.writeXml (Writer, System.Data.xmlwritemode.ignoreschema);

Return strbuilder.toString ();

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-54136.html

New Post(0)