Writing Dataset to XML corrupts XML file
-
I am having a lot of issues with this in VS2003 - programming in C# I am trying to update one field in an XML file using a dataset The code for the updating is _______________________________________________________________
// get the selected row DataRow selectedRow = contacts.Tables[0].Rows[contactlist.SelectedIndex]; // Begin an edit transaction on the row. selectedRow.BeginEdit(); selectedRow["Service_Release_SQL"] =sr_sql.Text; selectedRow.EndEdit(); contacts.WriteXml(@"contacts.xml");
______________________________________________________________ where "contactlist.SelectedIndex" gives me the XML row to update, and the field in that row is called "Service_Release_SQL" This complies OK, and runs without throwing an exception but the result always is that my XML file loses all field after the "Service_Release_SQL" field. Can anyone indicate where I am going wrong Thank you for your time -
I am having a lot of issues with this in VS2003 - programming in C# I am trying to update one field in an XML file using a dataset The code for the updating is _______________________________________________________________
// get the selected row DataRow selectedRow = contacts.Tables[0].Rows[contactlist.SelectedIndex]; // Begin an edit transaction on the row. selectedRow.BeginEdit(); selectedRow["Service_Release_SQL"] =sr_sql.Text; selectedRow.EndEdit(); contacts.WriteXml(@"contacts.xml");
______________________________________________________________ where "contactlist.SelectedIndex" gives me the XML row to update, and the field in that row is called "Service_Release_SQL" This complies OK, and runs without throwing an exception but the result always is that my XML file loses all field after the "Service_Release_SQL" field. Can anyone indicate where I am going wrong Thank you for your timeShould consult MSDN for this one, copied from there. You may be missing parts of this. private void WriteXmlToFile(DataSet thisDataSet) { // Create a file name to write to. string filename = "myXmlDoc.xml"; // Create the FileStream to write with. System.IO.FileStream myFileStream = new System.IO.FileStream (filename, System.IO.FileMode.Create); // Create an XmlTextWriter with the fileStream. System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode); // Write to the file with the WriteXml method. thisDataSet.WriteXml(myXmlWriter); myXmlWriter.Close(); }
-
Should consult MSDN for this one, copied from there. You may be missing parts of this. private void WriteXmlToFile(DataSet thisDataSet) { // Create a file name to write to. string filename = "myXmlDoc.xml"; // Create the FileStream to write with. System.IO.FileStream myFileStream = new System.IO.FileStream (filename, System.IO.FileMode.Create); // Create an XmlTextWriter with the fileStream. System.Xml.XmlTextWriter myXmlWriter = new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode); // Write to the file with the WriteXml method. thisDataSet.WriteXml(myXmlWriter); myXmlWriter.Close(); }
Thanks I suspect that my trouble is that the dataset is "set" to a different XML schema that the file being loaded (a previous version of the XML file), hence ignoring the columns after the ones it "knows about". I suspect I will need to specifically load the schema, and save the schema as well as the data. Will try it out on Monday when I get back to the office