判断XML文件是否存在,不存在就创建一个XMl文件,存在就在里面添加

时间:2022-01-30 14:01:18

if (File.Exists(xmlPath + "\\" + xmlName)) //判断文件是否寸在                 {     //存在的情况下                     XmlDocument xmldoc = new XmlDocument();                     xmldoc.Load(xmlPath + "\\" + xmlName);                     //存在文件                     XmlNode root = xmldoc.SelectSingleNode("History");                     XmlElement bak = xmldoc.createElement_x("Bak");                     root.AppendChild(bak);

                    XmlElement bakTime = xmldoc.createElement_x("BakTime");                     bakTime.InnerText = DateTime.Now.ToString();                     bak.AppendChild(bakTime); //备份时间

                    XmlElement bakName = xmldoc.createElement_x("BakName");                     bakName.InnerText = dbFileName;                     bak.AppendChild(bakName); // 备份的名字

                    XmlElement DataSource = xmldoc.createElement_x("DataSource");                     DataSource.InnerText = dataSource;                     bak.AppendChild(DataSource); //数据源

                    XmlElement Database = xmldoc.createElement_x("Database");                     Database.InnerText = database;                     bak.AppendChild(Database); //数据库名称

                    XmlElement DatabaseUser = xmldoc.createElement_x("UserID");                     DatabaseUser.InnerText = userID;                     bak.AppendChild(DatabaseUser); //数据库用户名

                    XmlElement Password = xmldoc.createElement_x("Password");                     Password.InnerText = password;                     bak.AppendChild(Password); // 数据库密码

                    XmlElement UserName = xmldoc.createElement_x("StaffName");                     UserName.InnerText = staffName;                     bak.AppendChild(UserName); //备份人

                    xmldoc.Save(xmlPath + "\\" + xmlName);

                }                 else                 {                     //不存在文件                     Directory.CreateDirectory(xmlPath);//创建该文件

                    XmlDocument xmldoc = new XmlDocument();

                    XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);                     xmldoc.AppendChild(xmlnode);

                    XmlElement root = xmldoc.createElement_x("History");                     xmldoc.AppendChild(root);                     XmlElement bak = xmldoc.createElement_x("Bak");                     root.AppendChild(bak);

                    XmlElement bakTime = xmldoc.createElement_x("BakTime");                     bakTime.InnerText = DateTime.Now.ToString();                     bak.AppendChild(bakTime); //备份时间

                    XmlElement bakName = xmldoc.createElement_x("BakName");                     bakName.InnerText = dbFileName;                     bak.AppendChild(bakName); // 备份的名字

                    XmlElement DataSource = xmldoc.createElement_x("DataSource");                     DataSource.InnerText = dataSource;                     bak.AppendChild(DataSource); //数据源

                    XmlElement Database = xmldoc.createElement_x("Database");                     Database.InnerText = database;                     bak.AppendChild(Database); //数据库

                    XmlElement DatabaseUser = xmldoc.createElement_x("UserID");                     DatabaseUser.InnerText = userID;                     bak.AppendChild(DatabaseUser); //数据库用户名

                    XmlElement Password = xmldoc.createElement_x("Password");                     Password.InnerText = password;                     bak.AppendChild(Password); // 数据库密码

                    XmlElement UserName = xmldoc.createElement_x("StaffName");                     UserName.InnerText = staffName;                     bak.AppendChild(UserName); //备份人

                    xmldoc.Save(xmlPath + "\\" + xmlName);                 }