How To Serialize Dataset In Vb.net

Posted By admin On 28/05/18
How To Serialize Dataset In Vb.net Average ratng: 4,3/5 3940reviews

If you need a collection and you're serializing the data, a DataSet is the tool to use. An Introduction to DataSet in VB.NET. Retrieved from https. Vb.net datatable Serialize to json. Browse other questions tagged json vb.net serialization datatable or ask your own question.

Give More Feedback

I searched high and wide for something simple to do this and every function I found basically reinvented the wheel from scratch. If you do any amount of searching, you can find the JavaScriptSerializer object which will convert objects to JSON. Sounds easy enough.WRONG! Even converting an empty DataSet will return a circular reference exception. On a similar note, I found several articles claiming to convert XML to JSON using the same function. The 'solution' was something like this: Dim jss As New JavaScriptSerializer Dim jsonString As String = jss.Serialize(DataSet1.GetXML()) I tried this thinking it made sense. But instead of serializing the data in the XML, it simply dumps the entire XML string into a single element in the JSON string.

I'm pretty sure that's not what anyone wants it to do. So how do you do it? Well, there are a number of methods, but I found the quickest and easiest is to just convert your DataSet to a Dictionary object, then use the JavaScriptSerializer to make the conversion.

See More On Stackoverflow

Kevininstructor 29-Jul-13 12:12 29-Jul-13 12:12 Thanks for sharing. I rarely use DataSet containers but instead use single DataTable containers. This is a good question and I needed the answer as well. Here is the modified function with a quick way to add column names. (I am working on nesting arrays based on relationships within the dataset as well if anyone is interested). Public Shared Function DataSetToJSON(ds As DataSet) As String Dim dict As New Dictionary( Of String, Object) Dim arrayList As New ArrayList For Each dt As DataTable In ds.Tables Dim arr(dt.Rows.Count) As Object For i As Integer = 0 To dt.Rows.Count - 1 arrayList.Clear() For Each oCol As DataColumn In dt.Columns arrayList.Add(oCol.ColumnName.ToLower & Chr( 34) & ': ' & Chr( 34) & dt.Rows(i).Item(oCol.ColumnName)) Next arr(i) = arrayList.ToArray Next dict.Add(dt.TableName.ToLower, arr) Next Dim json As New JavaScriptSerializer Return json.Serialize(dict) End Function.

Thanks a bunch for this.

8 Comments • what about the schema? Sometimes you have more than just table schema.

- • The constructor that is called during deserialization calls InitClass(), which adds all the tables, columns, relations, constraints, etc, that were originally defined in the typed dataset. So, all the schema information is kept. Pogoda Na Piatek Serial.

It won´t work if you changed the dataset definition in runtime in the caller, as those changes won´t be serialized. - • why not just do dataSet.WriteXML, then serialize the resultant string? E.m.magic Swf2avi 2008 V6.7.0.128 Full Crack more. You can add necessary arguments to ensure you have the complete XML, as well as optionally print out the schema I believe. Then re-hydrate on the other end. - • That's what the original serialization does. It works but it's slow and big. - • Great!!!!!!

Now, I had a problem, with untyped dataset I have to buy a ticket to the travel of the schema; what do u think about to create a channel sink that make a cache to the schema, and the second time that I bring the dataset doesn´t have to bring de schema? Pablo - • Very interresting.

In VB.NET I can't seem to override the DataSets own implementation of ISerializable i get the compiler error 'is already implemented by base class'. Any suggestions (other than use c#!) - • I used the surrogate wrapper solution presented in Microsoft KB article # 829740 with a small twist. John Deere 2653a Manual. I added a new member to stored the DataSet's type and then used reflection the typed dataset to recreate it during deserialziation. To get the data in without much code change, I used DataSet.Merge(). Seems to work fine!

- • Just to let you know if you replace the DBNull with Nothing in your worker array your data takes up dramatically less space when serialized (depending on how many DBNulls you have!) I am talking about storing datasets in viewstate of ASP.NET page, have not tried other serialization formatters.