Home > front end >  I want to back up the data in an xml file, I couldn't find how to save the newly received data
I want to back up the data in an xml file, I couldn't find how to save the newly received data

Time:03-17

I want to back up the data for today's date in an XML file every 10 minutes I managed to create the XML file, but I couldn't find how to save the newly received data without adding the same data to the existing file

Can I convert the file I created to dataSet with dataSet.ReadXml, add the new data I got from the query and convert it back to an XML file and save it? What method should I use?

String QueryString = "SELECT * FROM dbo.db_records WHERE DAY(datetime) = DAY(CURRENT_TIMESTAMP)";

        public void run()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(600000);
                    if (odbcConnection.State != ConnectionState.Open)
                    {
                        odbcConnection.Close();
                        odbcConnection.Open();
                    }

                    DataSet dataSet = new DataSet("XMLDB");
                    odbcDataAdapter.Fill(dataSet, "@ID");

                    if (File.Exists(Path))
                    {

                    }
                    else
                    {
                        using (FileStream fs = File.Create(Path))
                        {
                            dataSet.WriteXml(fs);
                        }
                    }
                }
                catch (Exception) { }
            }
        }

CodePudding user response:

Xml is not a great format if you want to append data, since it uses tags that need to be closed. So you have a few options:

Save separate files

Since you seem to fetch data for the current day, just attach date-info to your file-name. When reading the data you may need to read all files in the folder fitting the pattern, and merge it.

Use a format that is trivial to append

If your data model is simple tabular data you may use a .csv file instead. You can add data to this using one of the File.Append methods.

Overwrite all data

Get the complete data you want to save each time, and overwrite any existing data. This is simple, but may be slow if you have lots of data. But if the database is small and grow slowly this might be perfectly fine.

Parse the existing data

You could read the existing file with Readxml as you suggest, and use DataSet.Merge to merge it with your new set before overwriting the existing file. This may also be slow, since it needs to process all the data. But it may put less load on the database than fetching all data from the database each time.

In any case, you might want to periodically save full backups, or have some other way to handle corrupt files. You should also have some way to test the backups. I would also consider using the backup options built into most database engines if that is an alternative.

  • Related