views:

2445

answers:

6

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.

A: 

You can transfer DataTable's over a Web Service. So that is probably your best option, becuase that is what the client asked for.

Nick Berardi
+1  A: 

Sure. Both DataTable and DataSet are serializable, and they also have ReadXml() and WriteXml() functions you can use, too.

Joel Coehoorn
+2  A: 

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.

Marc Gravell
+2  A: 

See this post by Richard Blewett for reasons why you probably wouldn't want to do this.

Chris Ballard
+1  A: 

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

freggel
A: 

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.

Tor Haugen