We help IT Professionals succeed at work.

.NET DataRow Serialization

myevh3
myevh3 asked
on
I'm currently trying to figure out, short of rewriting all the objects, how to make a set of objects that are based on a datarow serializable.  The objects take strongly types datarows and expose the field values in the object properties:

Example:
Public string Name{
   Get{
      return this.DataRow.Name;
   }
}

The object classes use a number of property and methods of the data row for functionality.  Is there any simple changes that can be made to the struction of the class that would allow it to be serializable.  

Any help would be appreciated.

Thanks,
Comment
Watch Question

Top Expert 2010

Commented:

you can not xml-serialize DataRow as it doesn't have default constructor; but you can try the BinaryFormatter to serialize the whole object as a binary data if you want.

If it is possible in your requirment, other option is to serialize the DataTable of this DataRow and so after deserialize you can reconstruct your objects passing the DataRows
what i could figure out of your question is that you want your datarow object to expose all of its fields in the form of properties. And this has to happen dynamically at run time as each datarow object might have different columns in it.

Well this can be done, but is quite tricky to do this.
I will refer you one code project article that helps you do this.

The concept is called Light Weight Code Generation in .net.
It allows you to dynamically add code in your objects at run time using Reflection.Emit namespace.

Please refer to the following:-
http://www.codeproject.com/KB/dotnet/Creating_Dynamic_Types.aspx

please let me know if this helped.


Commented:
Hi,

you can try this

DataRow myRow;
DataTable myTable = new DataTable();
dt.ImportRow(myRow);
dt.WriteXml("C:\\myDataTable.xml");

and for reading

dt.ReadXml("C:\\myDataTable.xml");

makber

Author

Commented:
Only partially correct.