Dataset.WriteXML loses arrays / not preserving all data

  • Thread starter Thread starter Fergal Ó Ceallaigh
  • Start date Start date
F

Fergal Ó Ceallaigh

Hello,

I have a dataset with one table which has a single row. I need to save this
to disk as XML and do this successfully throughout my application. However,
one column in this table is a datatype Array which I use to store a bunch of
information.

I will simplify the code here:

*************
MyNewRow.Item(0) = "foo"
MyNewRow.Item(1) = OtherTable.Rows(0).ItemArray
MyClass.MyDataSet.Tables(0).Rows.Add(MyNewRow)

*************

When I go to the debug window and check that the data has been accepted it
appears that it has:


========Debug Window=========
?MyClass.MyDataSet.Tables(0).Rows(0).Item(1)
{System.Array}
(0): "26B70232-BC13-4247-9580-B12271B7EADE"
(1): "7924B3A4-6E59-4E87-8217-910D77353D41"

However, when attempt to persist this dataset to XML and look inside the XML
file, the data is lost and all that is left is the text:

<SomeColumn>System.Object[]</SomeColumn>

If I reload the dataset with the saved XML instead of my array I get
========Debug Window=========
?MyClass.MyDataSet.Tables(0).Rows(0).Item(1)
"System.Object[]" {String}
String: "System.Object[]"

I am sure the answer to this must be fairly simple but I can't find it as
yet. Any help is greatly appreciated.

Thank you,

Fergal.
 
Fergal,

A dataset is a kind of reflection from a relational database. In those are
no managed arrays (complex datatypes).

You will have to make a seperate (related) table for that when you want to
use it directly as an array.

I hope this helps,

Cor
 
Hi Cor, yes that helps. I am still a little puzzled: If it is not
supported, why does the Dataset accept the input and store it correctly? It
only seems to lose the information when I persist it to disk via writeXML.
That complex data type (in this case: Array) is available from that row/item
throughout the lifetime of the applciation - so it seems to support it, and
not support it. Anyone got a URL to some doco on this behaviour?

Thanks for you help,

Fergal.
 
Fergal,

I really don't know.

I tried what you told because I never noticed that it is in memory rigth,
however will not be serialized as it is in memory. This shows the behaviour.

\\\
Dim ds As New DataSet
Dim dt As New DataTable
dt.Columns.Add("Place", GetType(System.String()))
Dim a() As String = {"Whatever", "Whatelse"}
dt.LoadDataRow(New Object() {a}, True)
ds.Tables.Add(dt)
ds.WriteXml("c:\test1\whatever.xml")
MessageBox.Show(DirectCast(ds.Tables(0).Rows(0)(0), _
String())(0).ToString)
///

Sorry I don't know where this behaviour is documented.

Cor
 
Thanks Cor. So you were able to see that the array is preserved in memory
despite not being serialized? this is interesting. If I was desperate to
store this array in the row, I guess my only alternative would be to create
a class to wrap the array, serialize it to a stream and store it as a BLOB.
Sounds a bit harder then creating a new table. The data is being remoted so
I want to avoid as much overhead as possible. Whether that route would
result in a smaller footprint I do not know.

Regards,

- fergal.
 
Fergal,

That is not difficult. However I don't know if you want it.

Here a sample that I once made

\\\
Public Class Test
Public shared sub main
As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As New ArrayList
a.Add("Hello")
a.Add("I")
a.Add("am")
a.Add("Here")
Dim b As String = SerializeArraylist(a)
MessageBox.Show(b)
Dim c As ArrayList = DeserializeArraylist(b)
End Sub
Private Function SerializeArraylist(ByVal _
arraylst As ArrayList) As String
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(mem, arraylst)
Return Convert.ToBase64String(mem.ToArray())
End Function
Private Function DeserializeArraylist(ByVal _
arraystring As String) As ArrayList
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystring))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
End Class
///

I hope this helps a little bit?

Cor
 
Back
Top