Click here to Skip to main content
Sign Up to vote bad
good
See more: C#3.0
Hi Guys,
I have an problem,please tell me the correct solution.
Whenever my windows computer crashes,i loss my all xml data.
my C# program is using these two methods for reading and
writing data(Methods are given below).
 

How, i'm reading my data from XML File:
 
 static Mutex mutex;
 
 public static void AssignObjects()
        {
            if (mutex == null) { mutex = new Mutex(); }
        }
        public static string Get_ValueOfVariable(string VariableName,string Filename)
        {
            AssignObjects();
            mutex.WaitOne();
 
            string ValueIs = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XMLFileCheck.XMLDirectory + "\\" + Filename);
                string path = "Settings/Vars[Name='" + VariableName.Trim() + "']";
                XmlNode node = doc.SelectSingleNode(path);
                if (node != null)
                {
                    ValueIs = node.LastChild.InnerText;
                }
                else
                {
                    ValueIs = "";
                }
            }
            catch (Exception )
            {
                XMLFileCheck.CheckFor_CorruptedFile(new System.IO.FileInfo(Filename).Name);
            }
            mutex.ReleaseMutex();
            return ValueIs.Trim();
        }
 
How, i'm writing my data to XML File:
public static string InsertValue_OnVariable(string VariableName, string 
 
Value,string Filename)
        {
            AssignObjects();
            mutex.WaitOne();
 
            string Result = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(XMLFileCheck.XMLDirectory+"\\"+Filename);
 
                string path = "Settings/Vars[Name='" + VariableName.Trim() + "']";
                XmlNode node = doc.SelectSingleNode(path);
                node.LastChild.InnerText = Value.Trim();
 
                doc.Save(XMLFileCheck.XMLDirectory + "\\" + Filename);
               
            }
            catch (Exception)
            {
                XMLFileCheck.CheckFor_CorruptedFile(new System.IO.FileInfo(Filename).Name);
            }
 
            mutex.ReleaseMutex();
            return Result;
        }
Posted 12-Aug-12 4:19am
Edited 12-Aug-12 4:25am

Comments
Wes Aday - 12-Aug-12 10:25am
What does a system crash have to do with missing information? Does that mean that your app is running all the time and you want to save information when the computer crashes?
Aruny Yadav - 12-Aug-12 10:36am
Hi Wes Aday, Thanks for care. Just suppose, application is writing data using(doc.save() method). In the middle of writing-operation system crashes. Then i loss, my all old data and XML file becomes empty.
Richard MacCutchan - 12-Aug-12 11:42am
Find out why your system is crashing and fix the problem. What exactly do you expect to happen under that circumstance?
Aruny Yadav - 12-Aug-12 12:00pm
I want my xml file to be safe after system crash. But after crash my xml file becomes empty.
Richard MacCutchan - 12-Aug-12 12:30pm
Then you need to schedule your system crashes to occur at a time when you are not writing your XML file. Seriously, there is very little you can do in terms of protecting such a file from being lost in a system crash. There are strategies you can adopt to minimise the amount of data that may be lost, but there is no way of guaranteeing 100% protection. Maybe you would be better using some form of database rather than simple text files.

1 solution

If you really need to save to XML files your best bet is to keep the previous version also and not overwrite it (for this you can save a flag to disk to keep track if the new version has been written successfully).
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 514
1 Ron Beyer 296
2 OriginalGriff 248
3 samadhan_kshirsagar 229
4 Tadit Dash 213
0 Sergey Alexandrovich Kryukov 7,017
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,010


Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 12 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid