Serializing SQL CE data to XML
-
09-06-2019 - |
Question
I'm working on a product feature that will allow the user to export data from a SQL CE database on one copy of my application and re-import it into SQL CE on the other end. This data is not whole tables, but the result of queries.
I had hoped to take advantage of .net's built-in XML-based serialization like in DataTable.WriteXML. But, none of the methods for executing queries against a SqlCeCommand provide an obvious way of serializing to XML or extracting a DataTable, which could provide the method.
Is there something I'm missing? Do I have to write my own serialization-deserialization methods or is there a built-in way.
Solution
Assuming cmd is your SqlCeCommand....
using(var dr = cmd.ExecuteReader())
{
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add();
dt.Load(dr);
ds.WriteXML(...);
}
OTHER TIPS
You want to create an SqlCeDataAdapter
and use it .Fill()
a dataset. Then serialize the entire dataset via it's .WriteXml()
method.
I would think you could retrieve the data to a DataSet, call WriteXML on it, and then on the other end declare a new DataSet and call ReadXML on it.