一、基本操作:XmlDocument 写
class Program { static void Main(string[] args) { // 使用DOM操作,常用的类:XmlDocument、XmlElement、XmlAttribute和XmlText // 1.使用XmlDocument创建文档对象 XmlDocument xdoc = new XmlDocument(); // 2.创建文档描述 XmlDeclaration xdecl = xdoc.CreateXmlDeclaration("1.0", "utf-8", null); xdoc.AppendChild(xdecl); // 使用父节点.AppendChild(子节点) // 3.创建根节点 XmlElement xRoot = xdoc.CreateElement("personCollection"); xdoc.AppendChild(xRoot); // 4.创建person XmlElement person = xdoc.CreateElement("person"); xRoot.AppendChild(person); // 5.追加id属性 //XmlAttribute xid = xdoc.CreateAttribute("id"); //xid.Value = "001"; //person.SetAttributeNode(xid); person.SetAttribute("id", "001"); // 6.在person下面追加name、age和sex XmlElement name = xdoc.CreateElement("name"); person.AppendChild(name); XmlElement age = xdoc.CreateElement("age"); person.AppendChild(age); XmlElement sex = xdoc.CreateElement("sex"); person.AppendChild(sex); // 7.给name、age和sex等元素追加数据 XmlText xname = xdoc.CreateTextNode("admin"); name.AppendChild(xname); XmlText xage = xdoc.CreateTextNode("32"); age.AppendChild(xage); XmlText xsex = xdoc.CreateTextNode("男"); sex.AppendChild(xsex); // 保存文档 xdoc.Save("XmlDocumentDemo.xml"); } }
二、示例:
Users.xml文件:
<Users> <User>admin</User> <User>test</User> <User>emplorer</User> </Users>
using System; using System.Collections.Generic; using System.IO; using System.Xml; namespace ConsoleApplication1 { public class UserHelper { public static string BASEPATH = AppDomain.CurrentDomain.BaseDirectory + @"Users"; /// <summary> /// 所有的登录用户列表添加根节点 /// </summary> public static void InitLoginUsers() { string userspath = BASEPATH + @"All UsersUsers.xml"; if (!File.Exists(userspath)) { string allUserFolderPath = BASEPATH + @"All Users"; Directory.CreateDirectory(allUserFolderPath); FileStream fs = File.Create(userspath); fs.Close(); } // 1.使用XmlDocument创建文档对象 XmlDocument xmlDoc = new XmlDocument(); // 2.创建文档描述 //XmlDeclaration xxmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); //xmlDoc.AppendChild(xxmlDecl); // 使用父节点.AppendChild(子节点) XmlElement root = GetElement(xmlDoc, "Users"); root.InnerText = ""; xmlDoc.AppendChild(root); // 保存文档 xmlDoc.Save(userspath); } /// <summary> /// 判断节点是否存在(用于Users列表) /// 不存在 是就返回创建的一个新节点 /// 存在 返回相应的节点 /// </summary> /// <param name="xmlDoc"></param> /// <param name="node"></param> /// <returns></returns> private static XmlElement GetElement(XmlDocument xmlDoc, string node) { XmlElement xmlElement = null; if (xmlDoc.SelectSingleNode(node) == null) { xmlElement = xmlDoc.CreateElement(node); xmlElement.InnerText = ""; xmlDoc.AppendChild(xmlElement); // 创建根节点 //xml.DocumentElement.AppendChild(xmlElement); // 在根节点下添加子节点 } else { xmlElement = (XmlElement)xmlDoc.SelectSingleNode(node); } return xmlElement; } /// <summary> /// 用户列表 添加一个新用户 /// </summary> /// <param name="loginName"></param> public static void AddNewUser(string loginName) { string userspath = BASEPATH + @"All UsersUsers.xml"; XmlDocument xmlDoc = new XmlDocument(); //建立XmlDomcument对象 xmlDoc.Load(userspath); XmlNode root = GetElement(xmlDoc, "Users"); // 创建User XmlElement user = xmlDoc.CreateElement("User"); user.InnerText = loginName; root.AppendChild(user); // 保存文档 xmlDoc.Save(userspath); } /// <summary> /// 删除一个用户 /// </summary> /// <param name="loginname"></param> public static void DeleteUserByName(string loginname) { string userspath = BASEPATH + @"All UsersUsers.xml"; XmlDocument xmlDoc = new XmlDocument(); //建立XmlDomcument对象 xmlDoc.Load(userspath); XmlNode root = GetElement(xmlDoc, "Users"); XmlNodeList xList = root.SelectNodes("User"); foreach (XmlNode xNode in xList) { if (xNode.InnerText.Equals(loginname)) { xNode.ParentNode.RemoveChild(xNode); } } xmlDoc.Save(userspath); } /// <summary> /// 获取所有的用户信息 /// </summary> /// <returns></returns> public static List<string> SelectAllUsers() { List<string> listUsers = new List<string>(); string userinfopath = BASEPATH + @"All UsersUsers.xml"; if (File.Exists(userinfopath)) { XmlDocument xmlDoc = new XmlDocument(); if (!CheckFile(userinfopath, 0)) InitLoginUsers(); xmlDoc.Load(userinfopath); XmlNode root = GetElement(xmlDoc, "Users"); if (root != null) { XmlNodeList xList = root.SelectNodes("User"); foreach (XmlNode xNode in xList) { listUsers.Add(xNode.ChildNodes.Item(0).InnerText); } } xmlDoc.Save(userinfopath); } return listUsers; } /// <summary> /// 检验文件是否是初始化的 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="type">1是个人信息,0是用户列表</param> /// <returns>合法是TRUE,非法是FALSE</returns> public static bool CheckFile(string filePath, int type) { using (StreamReader sr = new StreamReader(filePath)) { string str = sr.ReadToEnd().Trim(); if (type == 1) { if (str.StartsWith("<PersonalConfig>") && str.Length > 32 && str.EndsWith("</PersonalConfig>")) { return true; } } else if (type == 0) { if (str.StartsWith("<Users>") && str.Length > 14 && str.EndsWith("</Users>")) { return true; } } } return false; } } }