码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-27 12:29:17      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:os   io   文件   ar   数据   div   sp   时间   new   

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

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

标签:os   io   文件   ar   数据   div   sp   时间   new   

原文地址:http://www.cnblogs.com/aersliing/p/3939085.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!