![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have an application that saves a bunch of data in XML files that come from typed datasets. So, I have something like this: MyTypedDs myds; //.... When the user wants to save the data myds.WriteXML(thefilename,XMLWriteMode.WriteSchema ); Later on, when he wants to open a saved file, myds=new (MyTypedDs); myds.ReadXML(thefilename,XMLReadMode.ReadSchema); Great. So far, so good. Now, some fields have been added to the records. The tables in the dataset have some additional columns. I want to be able to open up the old files that have been previously saved, and if the file is from the old format, convert it to the new format, filling in default values for missing fields. TypedDSV1 myversion1ds; TypedDSV2 myversion2ds; DataSet myds; //When opening a file. myds.ReadXML(thefilename,XMLReadMode.ReadSchema) if (the schema from the file matches version1) {myversion1ds=(TypedDSV1)myds; Convertv1tov2(myversion1ds,myversion2ds); } else if (the schema from the file matches version 2) { myversion2ds=(TypedDSV2)myds; } else return retvals.Schemadoesntmatch; So, is there a very easy way to write the (the schema from the file matches version 2) function? |
#3
| |||
| |||
|
|
Hi, you can try something like this in below sample code: private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); DataColumn col1 = new DataColumn(); DataColumn col2 = new DataColumn(); col1.ColumnName = "Name"; col2.ColumnName = "PostingTime"; col1.DataType = typeof(System.String); col2.DataType = typeof(System.DateTime); dt.Columns.Add(col1); dt.Columns.Add(col2); DataRow row1 = dt.NewRow(); row1["Name"] = "Manish Bafna"; row1["PostingTime"] = DateTime.Now; DataRow row2 = dt.NewRow(); row2["Name"] = "Sanjay Bafna"; row2["PostingTime"] = DateTime.Now.AddHours(5); dt.Rows.Add(row1); dt.Rows.Add(row2); dt.AcceptChanges(); DataSet ds = new DataSet(); ds.Tables.Add(dt); ds.WriteXml("XMLFile1.xml", XmlWriteMode.IgnoreSchema); dataGridView1.DataSource = dt; DataColumn col3 = new DataColumn(); col3.ColumnName = "NewColumn"; col3.DataType = typeof(System.String); col3.DefaultValue = "Hello"; ds.Tables[0].Columns.Add(col3); DataRow row3 = ds.Tables[0].NewRow(); row3["Name"] = "Janak NAIK"; row3["PostingTime"] = DateTime.Now.AddYears(2); row3["NewColumn"] = "Welcome"; ds.Tables[0].Rows.Add(row3); ds.AcceptChanges(); DataSet ds2 = new DataSet(); ds2.ReadXml("XMLFile1.xml", XmlReadMode.IgnoreSchema); ds.Merge(ds2, true, MissingSchemaAction.Ignore); dataGridView2.DataSource = ds.Tables[0]; } -- Hope this helps. Thanks and Regards. Manish Bafna. MCP and MCTS. "David" wrote: I have an application that saves a bunch of data in XML files that come from typed datasets. So, I have something like this: MyTypedDs myds; //.... When the user wants to save the data myds.WriteXML(thefilename,XMLWriteMode.WriteSchema ); Later on, when he wants to open a saved file, myds=new (MyTypedDs); myds.ReadXML(thefilename,XMLReadMode.ReadSchema); Great. So far, so good. Now, some fields have been added to the records. The tables in the dataset have some additional columns. I want to be able to open up the old files that have been previously saved, and if the file is from the old format, convert it to the new format, filling in default values for missing fields. TypedDSV1 myversion1ds; TypedDSV2 myversion2ds; DataSet myds; //When opening a file. myds.ReadXML(thefilename,XMLReadMode.ReadSchema) if (the schema from the file matches version1) {myversion1ds=(TypedDSV1)myds; Convertv1tov2(myversion1ds,myversion2ds); } else if (the schema from the file matches version 2) { myversion2ds=(TypedDSV2)myds; } else return retvals.Schemadoesntmatch; So, is there a very easy way to write the (the schema from the file matches version 2) function? |
#4
| |||
| |||
|
|
Thanks, although I was kind of hoping for a "compareschema" function. It would seem useful. I wonder why it doesn't exist. I think that, instead, what I really need is to see if the two datasets have the same number of tables and same columns. So, I think I'll write a utility that compares the columnames in each table. "Manish Bafna" wrote: Hi, you can try something like this in below sample code: private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); DataColumn col1 = new DataColumn(); DataColumn col2 = new DataColumn(); col1.ColumnName = "Name"; col2.ColumnName = "PostingTime"; col1.DataType = typeof(System.String); col2.DataType = typeof(System.DateTime); dt.Columns.Add(col1); dt.Columns.Add(col2); DataRow row1 = dt.NewRow(); row1["Name"] = "Manish Bafna"; row1["PostingTime"] = DateTime.Now; DataRow row2 = dt.NewRow(); row2["Name"] = "Sanjay Bafna"; row2["PostingTime"] = DateTime.Now.AddHours(5); dt.Rows.Add(row1); dt.Rows.Add(row2); dt.AcceptChanges(); DataSet ds = new DataSet(); ds.Tables.Add(dt); ds.WriteXml("XMLFile1.xml", XmlWriteMode.IgnoreSchema); dataGridView1.DataSource = dt; DataColumn col3 = new DataColumn(); col3.ColumnName = "NewColumn"; col3.DataType = typeof(System.String); col3.DefaultValue = "Hello"; ds.Tables[0].Columns.Add(col3); DataRow row3 = ds.Tables[0].NewRow(); row3["Name"] = "Janak NAIK"; row3["PostingTime"] = DateTime.Now.AddYears(2); row3["NewColumn"] = "Welcome"; ds.Tables[0].Rows.Add(row3); ds.AcceptChanges(); DataSet ds2 = new DataSet(); ds2.ReadXml("XMLFile1.xml", XmlReadMode.IgnoreSchema); ds.Merge(ds2, true, MissingSchemaAction.Ignore); dataGridView2.DataSource = ds.Tables[0]; } -- Hope this helps. Thanks and Regards. Manish Bafna. MCP and MCTS. "David" wrote: I have an application that saves a bunch of data in XML files that come from typed datasets. So, I have something like this: MyTypedDs myds; //.... When the user wants to save the data myds.WriteXML(thefilename,XMLWriteMode.WriteSchema ); Later on, when he wants to open a saved file, myds=new (MyTypedDs); myds.ReadXML(thefilename,XMLReadMode.ReadSchema); Great. So far, so good. Now, some fields have been added to the records. The tables in the dataset have some additional columns. I want to be able to open up the old files that have been previously saved, and if the file is from the old format, convert it to the new format, filling in default values for missing fields. TypedDSV1 myversion1ds; TypedDSV2 myversion2ds; DataSet myds; //When opening a file. myds.ReadXML(thefilename,XMLReadMode.ReadSchema) if (the schema from the file matches version1) {myversion1ds=(TypedDSV1)myds; Convertv1tov2(myversion1ds,myversion2ds); } else if (the schema from the file matches version 2) { myversion2ds=(TypedDSV2)myds; } else return retvals.Schemadoesntmatch; So, is there a very easy way to write the (the schema from the file matches version 2) function? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |