My experiences in calling Microsoft Web Services under Delphi7 (the service refers to the returned dataset)

Source: Internet
Author: User
Tags net xml

I learned how to call Microsoft Web Services under Delphi7. (The service refers to the returned dataset)
author: cowbird Date: 20040619

I wanted to write an articleArticleIt is used to express my disappointment with calling Web Services under Delphi7, because I have tried a lot of time and cannot implement the Web service that uses Delphi7 to call the returned data set under DOTNET.
I did my last attempt today, but I did not think it was a success.
The call method is a foreigner's article. After my experiments, I confirmed that the methods in the foreigner's articles were correct. The bad thing is that many small places did not mention it. It took me a lot of time. Here I wrote it in red, I hope you will not take any further detours.
Delphi calls the method to return the dataset Web Services:
Delphi receives soap through the httprio control. Then, write soap into an XML file and generate an XTR translation file using the XML mapper external tool. This XTR is used as the transformfile (translation file) of xmltransformprovider ). After the soap sent from httprio is passed to xmltransformprovider, xmtransformprovider can obtain data through the translation file (XTR) and provide it to clientdataset.
Is it troublesome? dataset in DOTNET can directly access data. However, I personally think that Delphi still has some advantages. After all, Win32ProgramThere is still a long time from the beginning to the official elimination. Even if the Longhorn system appears, there are still virtual machines running Win32 programs. Using Delphi7 in combination with Web Services Development can save time for learning other development tools, and using web services can also be reused for future development. If the project is time-consuming and developers are not familiar with the vs.net development environment, it is a transitional development method.

The following articles abstract in http://community.borland.com/article/0,1410,28631,00.html

Use ADO. Net datasets in Delphi
Ratings: Be the first! Rate it

Abstract:Learn how to use ADO. Net datasets in Delphi, using XML Mapper to transform XML into ss the platforms. demonstrated using a. NET web service and Delphi client.

Use ADO. Net datasets in Delphi

By Deepak Shenoy

Introduction

If you 've experimented with Web Services, you might have hit some Microsoft. net Based Web Services which return data all right, but it's in the default XML format from ADO. net. so you end up with some XML but you have no clue what to do with it! This article explains how you can take this XML, make sense out of it and even display it in a DB grid.

Scope

I'm not going to explain much about. net, or the ADO. net XML format. what I'll talk about is the most probable case you'll encounter. net Datasets: As XML returned from. net Based Web service. if you're going to use. net datasets in some other way, you might want to read this article to get an idea of how to make your Delphi application aware of them.

Hitting the. NET Service

Let's start with a simple. net service, as given in http://services.pagedownweb.com/ZipCodes.asmx. I 've used the Web Service importer in file | new | Other | Web Services and generated the Pascal files. here's the declaration that looks odd:

 
Rtnzipdsresult =Class(Tremotable)PrivateFs_schema:String;PublishedPropertyS_schema:String ReadFs_schemaWriteFs_schema;End; Zipcodessoap =Interface(Iinvokable) ['{FEF279A0-29EE-CF0B-FBB2-7DD79A5502CE}']...FunctionRtnzipds (ConstCity_in:String;ConstState_in:String): Rtnzipdsresult;Stdcall;...End;

TheRtnzipdsFunction returns a. Net dataset, as XML. Here,S_schemaOf the rtnzipdsresult class is simply the dataset as XML in ADO. net's default format. this means that. net client cocould easily get this XML and show it on a grid or a form-but can we do this with Delphi? Let's see.

Using the. NET service in Delphi

I 've created a sample form, which looks like this:

Now we 've ve setup the httprio, and the code behindGet zip codesButton is:

 
ProcedureTform1.button1click (Sender: tobject );Begin(Httprio1AsZipcodessoap). rtnzipds (edtcity. Text, edtstate. Text );End;

I 've also added an event handler on the httprio1.onafterexecute like so:

ProcedureTform1.httprio1afterexecute (ConstMethodname:String; Soapresponse: tstream );BeginSoapresponse. Position: = 0; memo1.lines. loadfromstream (soapresponse); soapresponse. Position: = 0;End;

This is only to display the returned content on to a memo so we can figure out what to do with it. Here's how the form looks now:

Interpreting the. NET XML

We have to figure out how to get Delphi to use this data. we wowould like to have a client data set read the XML so we can display it all in a grid. for that we'll have to use XDR transforms. no, that's not very complicated, and here's how we'll do it.

1. First we're going to save the XML returned into an XML file. I 've saved it as "data. xml ".
2. Run XML mapper from the Tools menu, and open this XML file. Here's a mega screen shot:

{When developing Web Services with C #, if you write oledbdataadapter1.fill (DS, 'tablename');}, you cannot see the fields in the orange basket above. Do not specify the table name in the dataset. You can write it in this way. oledbdataadapter1.fill (DS); then the field is displayed!

3. the zipdata (*) means there's multiple rows of "zipdata" available. columns availabl are zip, city, state, county and areacode. let's double-click each one of these to add them to the transformation and then clickDatapacket from XMLIn the create menu. Here's what it all looks like:

4. save the transformation using file | save | transformation, as "ziptrans. XTR ". don't try to test the transformation yet. (there's a bug in Delphi source code that doesn't like soap namespaces in certain elements so it doesn't show up any data ).

5. We'll now fix this bug. The XTR file is an XML file which you can open in any text editor. Open it, and change the first line from:

<Selecteach DEST = "datapacketrowdatarow" from = "Soap: envelopesoap: body .... "> [change to] <selecteach DEST =" datapacketrowdatarow "from =" envelopesoap: body .... ">

The reason for this is that Delphi's XML transform provider does not like the "Soap:" In the first element of the "from" attribute. that might get fixed in some update pack, so this point might not apply

{In addition to soap:, as long as the word is followed by a colon, the word and the colon must be removed before Delphi can display the data}

6. We're nearly there. DropTclientdataset,TxmltransformproviderAndTdatasourceOn the form. Here's what the form looks like now:

Link the grid, the datasource and the clientdataset, and set the clientdataset's providername to point to the XML transform provider. 7. SetTransformread. transformationfileOf the xmltransformproviderZiptrans. XTR.

8. Now we need to set the data of the XML transform provider at run time. Here's some additional code in the httprio's onafterexecute:

ProcedureTform1.httprio1afterexecute (ConstMethodname:String; Soapresponse: tstream );VaRXmldoc: ixmldocument;BeginSoapresponse. Position: = 0; memo1.lines. loadfromstream (soapresponse); clientdataset1.active: = false; soapresponse. Position: = 0; xmldoc: = newxmldocument; xmldoc. encoding: = suf8;// It should be 'suttf' and need to reference xmlintf, xmldoc two unitsSoapresponse. Position: = 0; xmldoc. loadfromstream (soapresponse); xmltransformprovider1.transformread. sourcexmldocument: = xmldoc. getdomdocument; clientdataset1.active: = true;End;

You'll notice that we 've ve created an XML document, loaded it from the encrypted soap stream, and applied the transform to it. the client dataset gets data from the provider and displays the data:

That's it!

Amazing.

Thank you.

What's next?

This transformation is very specific to this special service and XML schema. so if you know what XML is going to be returned (the format) Then you can use XML Mapper to generate a transformation for it.

I haven't been able to write a "general" transform that can be applied to any. Net returned XML, but if anyone does I 'd love to hear about it.

Also, why have I used the httprio's onafterexecute, rather than manipulating the s_schema parameter? There's another bug in Delphi that doesn't like parameters returned as XML. More revealed in this thread.

You can download all the code for this project at http://codecentral.borland.com/codecentral/ccweb.exe/listing? Id = 17807 or at http://www.agnisoft.com/soap/dotnetds.zip.

Deepak Shenoy (shenoy@agnisoft.com) is a director at Agni software, a software company in India that offers consultancy and offshore development solutions. it might be a while before his hair gets pointy so he's allowed to understand some technology.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.