How to persist data to an XML file when you add a data set using the add new item in VS 2005 RRS feed

  • Question

  • I needed to bind several controls on a Windows Form to a data set. Rather than manually create a data set and contained data tables, I created a data set in a project by using the add new item and choosing the "DataSet" template. I then added a data table to the data set in the designer. After that, I added columns to the data table, and then I created a BindingSource component in my form. The "DataSource" property of the BindingSource gets set to the data table that I created in the DataSet.


    As far as I can tell, there appears to be no easy way to call WriteXml on the data set. I cannot imagine that there is no way to write the DataSet to an Xml file and then reload it when the component is recreated. Does anyone have any suggestions as to how to do this?


    Thanks in advance!


    Best Regards,



    Wednesday, December 19, 2007 11:31 PM

All replies

  • What happens when you call WriteXml? Do you get an exception?

    Thursday, December 20, 2007 11:27 AM
  • Sorry. I should have posted that I figured out a way to do this.

    First, I created an "Init" method in the data set class. This init method first looks for an XML file in a specific location. It then reads the XML file if it exists by calling "ReadXmlSerializable" - which for the data set class, is generated by the designer and properly populates all data components in the DataSet with the objects stored in the Xml file.

    Then, the Init function subscribes to the RowChanged event of the DataTable contained in the DataSet class. In the RowChanged event handler, The data set's WriteXml function is called with the same file name as in the Init function except when "RowError" is true or the Row state is "Unchanged." In those cases, WriteXml is not called.

    Now here was the kind of tricky part.

    In "Load" method of the dialog where there are dialog components that are bound to the data set, I explicitly created an object of the DataSet class. I then call the "Init" method on the class. Finally, I set the DataSource property of the BindingSource object to the instance of the DataSet class I created in the Load method. Now, every time a change is made to a row in the bound data table, it is automatically written to an Xml file.

    The clue to figuring this out was to realize that in "InitializeComponent" a bound DataSet object seems to be not really created, i.e., you never have an instance of the object to begin with, so it is impossible to call any functions on the DataSet class as instanced in InitializeComponent. As far as I can tell, what InitializeComponent does is indicate that the object represented by the DataSource property of the BindingSource implements an Interface, but you really have no access to the class. So while it looks as if it operates properly, you really cannot call any methods on that class. While casting the DataSource property to the DataSet class compiles properly, the cast produces an invalid cast run time error when executed, but explicitly creating a DataSet class instance after InitializeComponent is called, then assigning that to the DataSource property of the BindingSource component works perfectly.

    Now that I figured this out, what would your method have been?


    Best Regards,

    Sunday, December 23, 2007 5:53 AM