Search code examples
c#xmllinq-to-xmlc1-cms

How to write multiple records at once in XML file?


While using XML based database, can I write multiple records at once to prevent excessive disk writes? I don't want to use XML file directly (to not prevent SQL migration - I might switch to SQL in the future).

I want to add 5000+ records -in a job queue- and update/delete them later, in a short period -when a job finished-.

While queue is processing I keep the record list in memory, at the end I need to write them back.

In the case of power loss, all completed jobs should be processed again, that's the con, but very rare, since the server is backed up by UPS.


Solution

  • DataConnection class supports overloads to add, delete or update multiple records at the same time.

    using(var conn = new DataConnection()) {
      var toBeUpdated = new List<MyDataType>();
      var toBeDeleted = new List<MyDataType>();
    
     var dataset = conn.Get<MyDataType>().ToList();
     for(var data in dataset) {
       if(...) {
        .....
        toBeUpdated.Add(data);
       }
     }
     .....
     .....
    
     conn.Update(toBeUpdated);
     conn.Delete(toBeDeleted);
    }