问题
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.
回答1:
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
回答2:
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.
回答3:
See this post by Richard Blewett for reasons why you probably wouldn't want to do this.
EDIT: To summarise the above:
- Using
DataSetisn't interoperable. It uses annotations which only make sense when each end of the link uses the Microsoft stack. DataSetis 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.
回答4:
Sure. Both DataTable and DataSet are serializable, and they also have ReadXml() and WriteXml() functions you can use, too.
回答5:
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;
}
回答6:
You can transfer DataTable's over a Web Service. So that is probably your best option, becuase that is what the client asked for.
回答7:
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.
回答8:
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.
来源:https://stackoverflow.com/questions/561203/can-i-serialize-a-data-table-or-data-set-to-transfer-over-a-web-service-in-c