I am using a web service to query data from a table. Then I have to send it to a user who wants it as a DataTable. Can I serialize the data? Or should I send it as A DataSet. I am new to Web Services, so I am not sure the best way to do it.
views:
2445answers:
6You can transfer DataTable's over a Web Service. So that is probably your best option, becuase that is what the client asked for.
Sure. Both DataTable and DataSet are serializable, and they also have ReadXml() and WriteXml() functions you can use, too.
If you expose it as a DataSet
/DataTable
, it will do its own serialization anyway (via IXmlSerializable
, IIRC). Note that DataSet
/DataTable
don't make for good data-types on web services if you want the service to be portable to other patforms (i.e. a java client, etc). But you can simply expose it as such if you want...; .NET will deal with the translation.
See this post by Richard Blewett for reasons why you probably wouldn't want to do this.
You can send the data as a xml string from a dataset by DataSet.GetXml()
And than the user can deserialize it with DataSet.ReadXml()
And get the datatable from the dataset by DataSet.Tables
Good luck
The simplest and most interoperable way is to serialize the dataset to XML with the GetXml() method, then pass that as a string from the web service. The client can then deserialize it with the ReadXml() method.
We have consumed a service which did it this way, and it worked great.
The alternative is to expose the DataSet class in the service, and return the dataset object. But this will complicate things, especially if any client should be non-.NET.