1、Ive created a sample form , which looks like this:Now weve setup the HTTPRio, and the code behind the Get Zip Codes button is: procedure TForm1.Button1Click(Sender: TObject);begin(HTTPRIO1 as ZipCodesSoap).rtnZipDS(edtCity.Text, edtState.Text);ve also added an event handler on the HTTPRio1.OnAfterEx
2、ecute like so:procedure TForm1.HTTPRIO1AfterExecute(const MethodName:SOAPResponse: TStream);SOAPResponse.Position := 0;Memo1.Lines.LoadFromStream(SOAPResponse);This is only to display the returned content on to a Memo so we can figure out what to do with it. Heres how the form looks now:Interpreting
3、 the .NET XML We have to figure out how to get Delphi to USE this data. We would like to have a Client Data Set read the XML so we can display it all in a Grid. For that well have to use XDR transforms. No, thats not very complicated, and heres how well do it. 1. First were going to save the XML ret
4、urned into an XML file. Ive saved it as data.xml.2. Run XML mapper from the Tools menu, and open this XML file. Heres a mega screen shot:3. The ZIPDATA(*) means theres multiple rows of ZIPDATA available. Columns availabl are Zip, City, State, County and AreaCode. Lets double-click each one of these
5、to add them to the transformation and then click the DataPacket from XML in the Create Menu. Heres what it all looks like:4. Save the Transformation using File | Save | Transformation, as Ziptrans.xtr. Dont try to test the transformation yet. (Theres a bug in Delphi Source code that doesnt like SOAP
6、 namespaces in certain elements so it doesnt show up any data). 5. Well 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:Change ToEnvelopesoap:The reason for this is that Delphis XML Transform provider does not like the soap
7、: in the first element of the from attribute. That might get fixed in some update pack, so this point might not apply 6. Were nearly there. Drop a TClientDataset, a TXMLTransformProvider and a TDatasource on the form. Heres what the form looks like now:Link the Grid, the Datasource and the ClientDat
8、aset, and set the ClientDatasets ProviderName to point to the XML Transform Provider. 7. Set the TransformRead.TransformationFile of the XMLTransformProvider to Ziptrans.XTR. 8. Now we need to set the data of the XML Transform Provider at run time. Heres some additional code in the HTTPRios OnAfterE
9、xecute:varXMLDoc: IXMLDocument;ClientDataset1.Active := FALSE;XMLDoc := NewXMLDocument;XMLDoc.Encoding := SUTF8;XMLDoc.LoadFromStream(SOAPResponse);XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument;= TRUE;Youll notice that weve created an XML Document , loaded it from th
10、e Received SOAP stream, and applied the transform to it. The client dataset gets data from the provider and displays the data :Thats it!Amazing.Thank you. Whats next?This transformation is very specific to this particular service and XML schema. SO if you know what XML is going to be returned (the f
11、ormat) then you can use XML mapper to generate a transformation for it. I havent been able to write a general transform that can be applied to ANY .NET returned XML, but if anyone does Id love to hear about it. Also, why have I used the HTTPRios OnAfterExecute, rather than manipulating the the s_sch
12、ema parameter? Theres another bug in Delphi that doesnt like parameters returned as XML. More revealed in this thread. You can download all the code for this project at or at Deepak Shenoy(shenoy) is a Director at Agni Software, a software company in India that offers consultancy and offshore develo
13、pment solutions. It might be a while before his hair gets pointy so hes allowed to understand some technology. 在微软中国找到了一个官方的说法-不建议将DataSet直接作为返回值传送,因为里面含有大量复杂的schema以及更改等信息,大部分非.NET语言在解析上有困难。建议使用DataSet.WriteXML方法将简化后的XML版本作为一个WideString回传。经过试验,已经在Delphi下轻松通过,Delphi中还需要使用XML Mapper工具事先生成Transfomatio
14、n(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(TF
15、orm) HTTPRIO1: THTTPRIO;Button1: TButton;Memo1: TMemo;XMLTransformProvider1: TXMLTransformProvider;ClientDataSet1: TClientDataSet;DataSource1: TDataSource;DBGrid1: TDBGrid;procedure Button1Click(Sender:private Private declarations public Public declarations var Form1: TForm1;implementation uses WSTe
16、stDefine;$R *.dfm A:Service1Soap;B:WideString;begin A := HTTPRIO1 as Service1Soap;B := A.GetPersonTable;Memo1.Lines.Add( B );XMLDoc.LoadFromXML(B);end. - .NET WebService代码 System.Text.StringBuilder strbuilder=new System.Text.StringBuilder();StringWriter writer=new StringWriter(strbuilder);dataset.Wr
17、iteXml(writer,System.Data.XmlWriteMode.IgnoreSchema);return strbuilder.ToString();class XmlDatasetConvert/将xml对象内容字符串转换为DataSetpublic static DataSet ConvertXMLToDataSet(string xmlData)StringReader stream = null;XmlTextReader reader = null;tryDataSet xmlDS = new DataSet();stream = new StringReader(xm
18、lData);/从stream装载到XmlTextReaderreader = new XmlTextReader(stream);xmlDS.ReadXml(reader);return xmlDS;catch (System.Exception ex)throw ex;finallyif (reader != null)reader.Close();/将xml文件转换为DataSetpublic static DataSet ConvertXMLFileToDataSet(string xmlFile)XmlDocument xmld = new XmlDocument();xmld.Lo
19、ad(xmlFile);stream = new StringReader(xmld.InnerXml);/xmlDS.ReadXml(xmlFile);/将DataSet转换为xml对象字符串public static string ConvertDataSetToXML(DataSet xmlDS)MemoryStream stream = null;XmlTextWriter writer = null;stream = new MemoryStream();writer = new XmlTextWriter(stream, Encoding.Unicode);/用WriteXml方法
20、写入文件.xmlDS.WriteXml(writer);int count = (int) stream.Length;byte arr = new bytecount;stream.Seek(0, SeekOrigin.Begin);stream.Read(arr, 0, count);UnicodeEncoding utf = new UnicodeEncoding();return utf.GetString(arr).Trim();if (writer !writer.Close();/将DataSet转换为xml文件public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)/返回Unicode编码的文本StreamWriter sw = new StreamWriter(xmlFile);sw.WriteLine(?xml version=1.0 encoding=utf-8);sw.WriteLine(utf.GetString(arr).Trim();sw.Close();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1