本文介绍了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数据在系统崩溃时丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:00
查看更多