XML数据在系统崩溃时丢失 [英] XML Data Lost On System Crash

查看:307
本文介绍了XML数据在系统崩溃时丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有问题,请告诉我正确的解决方法.
每当我的Windows计算机崩溃时,我都会丢失所有xml数据.
我的C#程序使用这两种方法进行读取和
写入数据(方法如下).


我如何从XML文件读取数据:

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();
       }



我如何将数据写入XML文件:



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;
        }

推荐答案

如果您确实需要保存到XML文件,则最好的选择是保留以前的版本,而不要覆盖它(为此,您可以将标记保存到磁盘以跟踪新版本是否已成功写入.)
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).


这篇关于XML数据在系统崩溃时丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆