11

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.

Loganj99
  • 449
  • 2
  • 9
  • 26

8 Answers8

9

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

Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109
freggel
  • 552
  • 2
  • 6
  • 13
7

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
7

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

EDIT: To summarise the above:

  • Using DataSet isn't interoperable. It uses annotations which only make sense when each end of the link uses the Microsoft stack.
  • DataSet is a pretty inefficient class for sending data over the wire - it contains change tracking data and metadata also.
  • It introduces tight coupling - changes made on the service side which might not even be needed by the client have to be reflected on the client side as well.
Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
  • 2
    seriously, a downvote because a link to an external site posted 4 years ago stopped working? relocated blog found and link updated – Chris Ballard Aug 06 '13 at 13:11
  • 1
    Sorry, I wasn't clear on the protocol and meaning of downvoting. I only meant to indicate that the link was broken. Downvote removed. – betitall Oct 09 '13 at 06:24
  • @betitall - thanks! actually since I wrote this answer, popular opinion is now that single sentence link answers are frowned upon, exactly because of what happened - the link goes stale so the answer no longer adds value. – Chris Ballard Mar 04 '14 at 15:17
5

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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
2

DataTable are serializable but some tricks exist to return a untyped DataTable through WebService.

Before web service method returns filled DataTable (on service side) it must have assigned property TableName. In case if inside the OperationContract DataTable receives the reference of typed DataTable the serialization will fail with messaga like ""The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error".

It means that you should fill the created DataTable, for instance using method Merge (or manually). Here are such DataContract implementation sample;

DataTable IHorizonCLService.RetrieveChangeLog(int iId)
{
   DataTable dt = new DataTable(); //create new DataTable to be returned by this web method
   dt.Merge(HorMan.RetrieveChangeLog(iId)); //get typed DataTable and fills the DataTable 
   dt.TableName = "SurChangeLogHor"; //assigns name
   return dt;
}
Lesh
  • 21
  • 1
1

Just an interesting remark from here

And, never, ever, ever send an instance of System.Data.DataSet over the wire. There has never been, is not now, and never will be any reason to ever send any instance of this type anywhere. It's beyond massive and makes the 10,000 property data transfer object seem lightweight. The fact that something is serializable doesn't mean that it should be.

Painy James
  • 805
  • 2
  • 13
  • 26
1

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
  • 54,393
  • 15
  • 113
  • 135
1

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
  • 19,509
  • 9
  • 45
  • 63