XML round trip problems
-
I'm writing out a dataset...
dsPrefs.WriteXml(ManagerPrefsFilename, XmlWriteMode.WriteSchema);
and, at other times reading it in...dsPrefs.ReadXml(ManagerPrefsFilename,XmlReadMode.IgnoreSchema);
The problem is that one of the columns is of type Point. The XML written by .WriteXml seems to contain all the info needed to describe this. Yet when I read it back in with .ReadXml and check the type it is System.String. I've tried different XmlReadMode to no avail. Any idea why these aren't being converted to the correct type when read in? -- James -- -
I'm writing out a dataset...
dsPrefs.WriteXml(ManagerPrefsFilename, XmlWriteMode.WriteSchema);
and, at other times reading it in...dsPrefs.ReadXml(ManagerPrefsFilename,XmlReadMode.IgnoreSchema);
The problem is that one of the columns is of type Point. The XML written by .WriteXml seems to contain all the info needed to describe this. Yet when I read it back in with .ReadXml and check the type it is System.String. I've tried different XmlReadMode to no avail. Any idea why these aren't being converted to the correct type when read in? -- James --Because you're ignoring the schema ? Does the XML mark the variable as being of type point, with an attribute or something ? Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
Because you're ignoring the schema ? Does the XML mark the variable as being of type point, with an attribute or something ? Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
Here's a simplified example. It creates an Xml file named "XmlRoundTrip.xml" on the Desktop from a DataSet and then Reads it back in. Just add the code below to the constructor of a default Form template and run it. The first Message box shows "Type written: System.Drawing.Point". The dataset is then cleared and the data is re-read. But the second Message box shows "Type read: System.String". Any ideas? Am I expecting too much from the DS XML methods?
// Create a DataSet DataSet ds = new DataSet("MyDataSet"); // Create a Table for the DataSet DataTable dt = ds.Tables.Add("MyDataTable"); // Definte two columns for the DataTable DataColumn dcA = new DataColumn("PointColumn", typeof(Point)); dcA.DefaultValue = new Point(8, 13); DataColumn dcB = new DataColumn("SizeColumn", typeof(Size)); dcB.DefaultValue = new Size(21, 34); // Add the columns to the DataDable dt.Columns.Add(dcA); dt.Columns.Add(dcB); // Create a new row with default values and add it to the table DataRow dr = dt.NewRow(); dt.Rows.Add(dr); // Build file name for Xml file. string myTempFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string myXmlFilename = myTempFolder + "\\XmlRoundTrip.xml"; // Write the Xml file. ds.WriteXml(myXmlFilename, XmlWriteMode.WriteSchema); // Confirm the type of the first column. MessageBox.Show("Type written: " + dt.Rows[0]["PointColumn"].GetType().ToString()); // Clear the table dt.Clear(); // Read the data from the Xml file. ds.ReadXml(myXmlFilename, XmlReadMode.ReadSchema); // Inspect read type. MessageBox.Show("Type read: " + dt.Rows[0]["PointColumn"].GetType().ToString());
-- James -- -
Here's a simplified example. It creates an Xml file named "XmlRoundTrip.xml" on the Desktop from a DataSet and then Reads it back in. Just add the code below to the constructor of a default Form template and run it. The first Message box shows "Type written: System.Drawing.Point". The dataset is then cleared and the data is re-read. But the second Message box shows "Type read: System.String". Any ideas? Am I expecting too much from the DS XML methods?
// Create a DataSet DataSet ds = new DataSet("MyDataSet"); // Create a Table for the DataSet DataTable dt = ds.Tables.Add("MyDataTable"); // Definte two columns for the DataTable DataColumn dcA = new DataColumn("PointColumn", typeof(Point)); dcA.DefaultValue = new Point(8, 13); DataColumn dcB = new DataColumn("SizeColumn", typeof(Size)); dcB.DefaultValue = new Size(21, 34); // Add the columns to the DataDable dt.Columns.Add(dcA); dt.Columns.Add(dcB); // Create a new row with default values and add it to the table DataRow dr = dt.NewRow(); dt.Rows.Add(dr); // Build file name for Xml file. string myTempFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string myXmlFilename = myTempFolder + "\\XmlRoundTrip.xml"; // Write the Xml file. ds.WriteXml(myXmlFilename, XmlWriteMode.WriteSchema); // Confirm the type of the first column. MessageBox.Show("Type written: " + dt.Rows[0]["PointColumn"].GetType().ToString()); // Clear the table dt.Clear(); // Read the data from the Xml file. ds.ReadXml(myXmlFilename, XmlReadMode.ReadSchema); // Inspect read type. MessageBox.Show("Type read: " + dt.Rows[0]["PointColumn"].GetType().ToString());
-- James --Good point - the XML generated is bursting with type information, so there's no reason I can see for this not to work, but it doesn't. How pathetic. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder