﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-玄铁剑-文章分类-Delphi相关</title><link>http://www.cnitblog.com/MartinYao/category/4538.html</link><description>成功的途径：抄，创造，研究，发明...</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 12:56:35 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 12:56:35 GMT</pubDate><ttl>60</ttl><item><title>Delphi调用Webservice[DataTable/DataSet]</title><link>http://www.cnitblog.com/MartinYao/articles/20874.html</link><dc:creator>玄铁剑</dc:creator><author>玄铁剑</author><pubDate>Wed, 20 Dec 2006 13:30:00 GMT</pubDate><guid>http://www.cnitblog.com/MartinYao/articles/20874.html</guid><wfw:comment>http://www.cnitblog.com/MartinYao/comments/20874.html</wfw:comment><comments>http://www.cnitblog.com/MartinYao/articles/20874.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/MartinYao/comments/commentRss/20874.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/MartinYao/services/trackbacks/20874.html</trackback:ping><description><![CDATA[<div class=ArticleTitle><font face=Verdana><strong>Use ADO.NET Datasets in Delphi</strong> </font></div>
<div class=ArticleBody>
<p>&nbsp;</p>
<p><strong><font face=Verdana size=2>Abstract</font> </strong></p>
<p>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. </p>
<p><span class=heading3><strong>Scope</strong> </span></p>
<p>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 a .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. </p>
<p><span class=heading3><strong>Hitting the .NET service</strong> </span></p>
<p>Let's start with a simple .NET service, as given in <a href="http://services.pagedownweb.com/ZipCodes.asmx">http://services.pagedownweb.com/ZipCodes.asmx</a>. 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: </p>
<pre class=sourcecode>						<code>
rtnZipDSResult = <strong>class</strong>(TRemotable)
<strong>private</strong>
Fs_schema: <strong>String</strong>;
<strong>published</strong><strong>property</strong> s_schema: <strong>String</strong><strong>read</strong> Fs_schema <strong>write</strong> Fs_schema;
<strong>end</strong>;
ZipCodesSoap = <strong>interface</strong>(IInvokable)
[<font color=#9933cc>'{FEF279A0-29EE-CF0B-FBB2-7DD79A5502CE}'</font>]
...
<strong>function</strong>  rtnZipDS(<strong>const</strong> City_IN: <strong>String</strong>; <strong>const</strong> State_IN: <strong>String</strong>): rtnZipDSResult; <strong>stdcall</strong>;
...
<strong>end</strong>;
</code>
</pre>
<p>The <strong>rtnZipDS</strong> function returns a .NET dataset, as XML. Here, the <strong>s_schema</strong> of the rtnZipDSResult class is simply the dataset as XML in ADO.NET's default format. This means that a .NET client could easily get this XML and show it on a grid or a form - but can we do this with Delphi? Let's see. </p>
<p><span class=heading3><strong>Using the .NET service in Delphi</strong> </span></p>
<p>I've created a sample form , which looks like this:<br><img src="http://www.agnisoft.com/soap/images/design.gif"></p>
<p>Now we've setup the HTTPRio, and the code behind the <strong>Get Zip Codes</strong> button is: </p>
<pre class=sourcecode>						<code>
<strong>procedure</strong> TForm1.Button1Click(Sender: TObject);
<strong>begin</strong>
(HTTPRIO1 <strong>as</strong> ZipCodesSoap).rtnZipDS(edtCity.Text, edtState.Text);
<strong>end</strong>;
</code>
</pre>
<p>I've also added an event handler on the HTTPRio1.OnAfterExecute like so: </p>
<pre class=sourcecode>						<code>
<strong>procedure</strong> TForm1.HTTPRIO1AfterExecute(<strong>const</strong> MethodName: <strong>String</strong>;
SOAPResponse: TStream);
<strong>begin</strong>
SOAPResponse.Position := 0;
Memo1.Lines.LoadFromStream(SOAPResponse);
SOAPResponse.Position := 0;
<strong>end</strong>;
</code>
</pre>
<p>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: </p>
<p><img src="http://www.agnisoft.com/soap/images/rawdata.gif"> </p>
<p><span class=heading3><strong>Interpreting the .NET XML</strong> </span></p>
<p>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 we'll have to use XDR transforms. No, that's not very complicated, and here's how we'll do it. </p>
<p>1. First we're going to save the XML returned into an XML file. I've saved it as "data.xml".<br>2. Run XML mapper from the Tools menu, and open this XML file. Here's a mega screen shot: </p>
<p><img src="http://www.agnisoft.com/soap/images/mapper1.gif"> </p>
<p>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 click the <strong>DataPacket from XML</strong> in the Create Menu. Here's what it all looks like: </p>
<p><img src="http://www.agnisoft.com/soap/images/mapper2.gif"> </p>
<p>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). </p>
<p>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:<br></p>
<pre class=sourcecode>						<code>
&lt;SelectEach dest="DATAPACKET\ROWDATA\ROW" from="\soap:Envelope\soap:Body\...."&gt;
[Change To]
&lt;SelectEach dest="DATAPACKET\ROWDATA\ROW" from="\Envelope\soap:Body\...."&gt;
</code>
</pre>
<em>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</em>
<p>6. We're nearly there. Drop a <strong>TClientDataset</strong>, a <strong>TXMLTransformProvider</strong> and a <strong>TDatasource</strong> on the form. Here's what the form looks like now: </p>
<p><img src="http://www.agnisoft.com/soap/images/design2.gif"> </p>
<p>Link the Grid, the Datasource and the ClientDataset, and set the ClientDataset's ProviderName to point to the XML Transform Provider. 7. Set the <strong>TransformRead.TransformationFile</strong> of the XMLTransformProvider to <strong>Ziptrans.XTR</strong>. </p>
<p><img src="http://www.agnisoft.com/soap/images/OI.gif"> </p>
<p>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: </p>
<p>&nbsp;</p>
<pre class=sourcecode>						<code>
<strong>procedure</strong> TForm1.HTTPRIO1AfterExecute(<strong>const</strong> MethodName: <strong>String</strong>;
SOAPResponse: TStream);
<strong>var</strong>
XMLDoc: IXMLDocument;
<strong>begin</strong>
SOAPResponse.Position := 0;
Memo1.Lines.LoadFromStream(SOAPResponse);
ClientDataset1.Active := FALSE;
SOAPResponse.Position := 0;
XMLDoc := NewXMLDocument;
XMLDoc.Encoding := SUTF8;
SOAPResponse.Position := 0;
XMLDoc.LoadFromStream(SOAPResponse);
XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument;
ClientDataset1.Active := TRUE;
<strong>end</strong>;
</code>
</pre>
You'll notice that we've created an XML Document , loaded it from the Received SOAP stream, and applied the transform to it. The client dataset gets data from the provider and displays the data :
<p><img src="http://www.agnisoft.com/soap/images/cdsresult.gif"></p>
<p>&nbsp;</p>
<p>That's it! </p>
<p><span class=heading3><strong>Amazing.</strong></span></p>
<p>Thank you. </p>
<p><span class=heading3><strong>What's next?</strong></span></p>
<p>This transformation is very specific to this particular 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. </p>
<p>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. </p>
<p>Also, why have I used the HTTPRio's OnAfterExecute, rather than manipulating the the s_schema parameter? There's another bug in Delphi that doesn't like parameters returned as XML. More revealed in <a href="http://groups.google.com/groups?hl=en&amp;ie=ISO-8859-1&amp;oe=ISO-8859-1&amp;selm=3c76a00f%241_1%40dnews">this thread</a>. </p>
<p>You can download all the code for this project at <a href="http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=17807">http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=17807</a> or at <a href="http://www.agnisoft.com/soap/dotnetds.zip">http://www.agnisoft.com/soap/dotnetds.zip</a>. </p>
<p><em><strong>Deepak Shenoy</strong>(<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#104;&#101;&#110;&#111;&#121;&#64;&#97;&#103;&#110;&#105;&#115;&#111;&#102;&#116;&#46;&#99;&#111;&#109;">shenoy@agnisoft.com</a>) is a Director at <a href="http://www.agnisoft.com/">Agni Software</a>, 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. <!-- content ends here--></em></p>
</div>
<p>在微软中国找到了一个官方的说法------不建议将DataSet直接作为返回值传送，因为里面含有大量复杂的schema以及更改等信息，大部分非.NET语言在解析上有困难。建议使用DataSet.WriteXML方法将简化后的XML版本作为一个WideString回传。经过试验，已经在Delphi下轻松通过，Delphi中还需要使用XML Mapper工具事先生成Transfomation(XTR)文件。 <br><br><strong>Delphi7客户端代码</strong> <br>---------------------------------------------------------------------------------------------------------------------------- <br><span style="COLOR: #333399"><strong>unit</strong></span> WSTestMain; <br><br><span style="COLOR: #333399"><strong>interface <br></strong></span><br><span style="COLOR: #333399"><strong>uses <br></strong></span>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, <br>&nbsp; Dialogs, InvokeRegistry, StdCtrls, Rio, SOAPHTTPClient, Grids, DBGrids, <br>&nbsp; DB, DBClient, DBTables, Provider, xmldom, Xmlxform,XMLIntf,XMLDoc,SOAPConst; <br><br><span style="COLOR: #333399"><strong>type <br></strong></span>&nbsp; TForm1 = class(TForm) <br>&nbsp; &nbsp; HTTPRIO1: THTTPRIO; <br>&nbsp; &nbsp; Button1: TButton; <br>&nbsp; &nbsp; Memo1: TMemo; <br>&nbsp; &nbsp; XMLTransformProvider1: TXMLTransformProvider; <br>&nbsp; &nbsp; ClientDataSet1: TClientDataSet; <br>&nbsp; &nbsp; DataSource1: TDataSource; <br>&nbsp; &nbsp; DBGrid1: TDBGrid; <br>&nbsp;&nbsp;&nbsp; <span style="COLOR: #333399"><strong>procedure</strong></span> Button1Click(Sender: TObject); <br>&nbsp; <span style="COLOR: #333399"><strong>private <br></strong></span>&nbsp;&nbsp;&nbsp; { Private declarations } <br>&nbsp; <span style="COLOR: #333399"><strong>public <br></strong></span>&nbsp;&nbsp;&nbsp; { Public declarations } <br>&nbsp; <span style="COLOR: #333399"><strong>end; <br></strong></span><br><span style="COLOR: #333399"><strong>var</strong></span> <br>&nbsp; Form1: TForm1; <br><br><span style="COLOR: #333399"><strong>implementation <br></strong></span><br><span style="COLOR: #333399"><strong>uses</strong></span> WSTestDefine; <br><br>{$R *.dfm} <br><br><span style="COLOR: #333399"><strong>procedure</strong></span> TForm1.Button1Click(Sender: TObject); <br><span style="COLOR: #333399"><strong>var</strong></span> <br>&nbsp; A:Service1Soap; <br>&nbsp; B:WideString; <br>&nbsp; XMLDoc: IXMLDocument; <br><span style="COLOR: #333399"><strong>begin <br></strong></span>&nbsp; A := HTTPRIO1 as Service1Soap; <br>&nbsp; B := A.GetPersonTable; <br>&nbsp; Memo1.Lines.Add( B ); <br>&nbsp; ClientDataset1.Active := FALSE; <br>&nbsp; XMLDoc := NewXMLDocument; <br>&nbsp; XMLDoc.Encoding := SUTF8; <br>&nbsp; XMLDoc.LoadFromXML(B); <br>&nbsp; XMLTransformProvider1.TransformRead.SourceXmlDocument := XMLDoc.GetDOMDocument; <br>&nbsp; ClientDataset1.Active := TRUE; <br><span style="COLOR: #333399"><strong>end;</strong></span> <br><br><span style="COLOR: #333399"><strong>end.</strong></span> <br>--------------------------------------------------------------------------------------- <br><strong>.NET WebService代码</strong> <br>--------------------------------------------------------------------------------------- <br>&nbsp;&nbsp; System.Text.StringBuilder strbuilder=new System.Text.StringBuilder(); <br>&nbsp; &nbsp;StringWriter writer=new StringWriter(strbuilder); <br>&nbsp; &nbsp;dataset.WriteXml(writer,System.Data.XmlWriteMode.IgnoreSchema); <br>&nbsp;&nbsp;&nbsp;return strbuilder.ToString(); <br></p>
<p>&nbsp;&nbsp;&nbsp; <strong class=k>class</strong> XmlDatasetConvert<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//将xml对象内容字符串转换为DataSet<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>public</strong> <strong class=k>static</strong> DataSet ConvertXMLToDataSet(<strong class=k>string</strong> xmlData)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringReader stream = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlTextReader reader = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>try</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet xmlDS = <strong class=k>new</strong> DataSet();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream = <strong class=k>new</strong> StringReader(xmlData);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//从stream装载到XmlTextReader<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader = <strong class=k>new</strong> XmlTextReader(stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlDS.ReadXml(reader);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>return</strong> xmlDS;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>catch</strong> (System.Exception ex)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>throw</strong> ex;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>finally</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>if</strong> (reader != <strong class=k>null</strong>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//将xml文件转换为DataSet<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>public</strong> <strong class=k>static</strong> DataSet ConvertXMLFileToDataSet(<strong class=k>string</strong> xmlFile)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StringReader stream = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlTextReader reader = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>try</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlDocument xmld = <strong class=k>new</strong> XmlDocument();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmld.Load(xmlFile);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet xmlDS = <strong class=k>new</strong> DataSet();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream = <strong class=k>new</strong> StringReader(xmld.InnerXml);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//从stream装载到XmlTextReader<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader = <strong class=k>new</strong> XmlTextReader(stream);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlDS.ReadXml(reader);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//xmlDS.ReadXml(xmlFile);<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>return</strong> xmlDS;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>catch</strong> (System.Exception ex)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>throw</strong> ex;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>finally</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>if</strong> (reader != <strong class=k>null</strong>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//将DataSet转换为xml对象字符串<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>public</strong> <strong class=k>static</strong> <strong class=k>string</strong> ConvertDataSetToXML(DataSet xmlDS)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemoryStream stream = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlTextWriter writer = <strong class=k>null</strong>;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>try</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream = <strong class=k>new</strong> MemoryStream();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//从stream装载到XmlTextReader<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer = <strong class=k>new</strong> XmlTextWriter(stream, Encoding.Unicode);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//用WriteXml方法写入文件.<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlDS.WriteXml(writer);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>int</strong> count = (<strong class=k>int</strong>) stream.Length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>byte</strong>[] arr = <strong class=k>new</strong> <strong class=k>byte</strong>[count];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream.Seek(0, SeekOrigin.Begin);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream.Read(arr, 0, count);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UnicodeEncoding utf = <strong class=k>new</strong> UnicodeEncoding();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>return</strong> utf.GetString(arr).Trim();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>catch</strong> (System.Exception ex)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>throw</strong> ex;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>finally</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>if</strong> (writer != <strong class=k>null</strong>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//将DataSet转换为xml文件<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>public</strong> <strong class=k>static</strong> <strong class=k>void</strong> ConvertDataSetToXMLFile(DataSet xmlDS, <strong class=k>string</strong> xmlFile)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemoryStream stream = <strong class=k>null</strong>;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XmlTextWriter writer = <strong class=k>null</strong>;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>try</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream = <strong class=k>new</strong> MemoryStream();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//从stream装载到XmlTextReader<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer = <strong class=k>new</strong> XmlTextWriter(stream, Encoding.Unicode);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//用WriteXml方法写入文件.<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlDS.WriteXml(writer);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>int</strong> count = (<strong class=k>int</strong>) stream.Length;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>byte</strong>[] arr = <strong class=k>new</strong> <strong class=k>byte</strong>[count];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream.Seek(0, SeekOrigin.Begin);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stream.Read(arr, 0, count);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=c>//返回Unicode编码的文本<br></strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; UnicodeEncoding utf = <strong class=k>new</strong> UnicodeEncoding();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StreamWriter sw = <strong class=k>new</strong> StreamWriter(xmlFile);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw.WriteLine(<strong class=s>"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;"</strong>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw.WriteLine(utf.GetString(arr).Trim());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sw.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>catch</strong> (System.Exception ex)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>throw</strong> ex;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>finally</strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong class=k>if</strong> (writer != <strong class=k>null</strong>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; writer.Close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br></p>
<img src ="http://www.cnitblog.com/MartinYao/aggbug/20874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/MartinYao/" target="_blank">玄铁剑</a> 2006-12-20 21:30 <a href="http://www.cnitblog.com/MartinYao/articles/20874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>