XmlDocument操作

Users.xml文件:

<Users>
  <User>admin</User>
  <User>test</User>
  <User>emplorer</User>
</Users>
XmlDocument操作
        public static string BASEPATH = AppDomain.CurrentDomain.BaseDirectory + @"Users";
        
      /// <summary>
        /// 所有的登录用户列表添加根节点
        /// </summary>
        public static void InitLoginUsers()
        {
            string userspath = BASEPATH + @"\All Users\Users.xml";
            if (!File.Exists(userspath))
            {
                string allUserFolderPath = BASEPATH + @"\All Users";
                Directory.CreateDirectory(allUserFolderPath);
                FileStream fs = File.Create(userspath);
                fs.Close();
            }
            XmlDocument xml = new XmlDocument();                     //建立XmlDomcument对象  
            XmlElement root = GetElement(xml, "Users");
            root.InnerText = "";
            xml.AppendChild(root);
            xml.Save(userspath);
        }

        /// <summary>
        /// 判断节点是否存在(用于Users列表)
        /// 不存在 是就返回创建的一个新节点
        /// 存在   返回相应的节点
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        private static XmlElement GetElement(XmlDocument xml, string node)
        {
            XmlElement xmlElement = null;
            if (xml.SelectSingleNode(node) == null)
            {
                xmlElement = xml.CreateElement(node);
                xmlElement.InnerText = "";
                xml.AppendChild(xmlElement);    // 添加根节点
             //xml.DocumentElement.AppendChild(xmlElement);    // 在根节点下添加子节点
            }
            else
            {
                xmlElement = (XmlElement)xml.SelectSingleNode(node);
            }
            return xmlElement;
        }

        /// <summary>
        /// 用户列表 添加一个新用户
        /// </summary>
        /// <param name="loginName"></param>
        public static void AddNewUser(string loginName)
        {
            string userspath = BASEPATH + @"\All Users\Users.xml";
            XmlDocument xml = new XmlDocument();                        //建立XmlDomcument对象  
            xml.Load(userspath);
            XmlNode root = GetElement(xml, "Users");

            XmlElement xmlloginname = xml.CreateElement("User");
            xmlloginname.InnerText = loginName;
            root.AppendChild(xmlloginname);
            xml.Save(userspath);

        }

        /// <summary>
        /// 删除一个用户
        /// </summary>
        /// <param name="loginname"></param>
        public static void DeleteUserByName(string loginname)
        {
            string userspath = BASEPATH + @"\All Users\Users.xml";
            XmlDocument xml = new XmlDocument();                        //建立XmlDomcument对象  
            xml.Load(userspath);
            XmlNode root = GetElement(xml, "Users");
            XmlNodeList xList = root.SelectNodes("User");

            foreach (XmlNode xNode in xList)
            {
                if (xNode.InnerText.Equals(loginname))
                {
                    xNode.ParentNode.RemoveChild(xNode);
                }
            }
            xml.Save(userspath);
        }

        /// <summary>
        /// 获取所有的用户信息
        /// </summary>
        /// <returns></returns>
        public static List<string> SelectAllUsers()
        {
            List<string> listUsers = new List<string>();

            string userinfopath = BASEPATH + @"\All Users\Users.xml";
            if (File.Exists(userinfopath))
            {
                XmlDocument xml = new XmlDocument();

                if (!CheckFile(userinfopath, 0))
                    InitLoginUsers();
                xml.Load(userinfopath);

                XmlNode root = GetElement(xml, "Users");
                if (root != null)
                {
                    XmlNodeList xList = root.SelectNodes("User");

                    foreach (XmlNode xNode in xList)
                    {
                        listUsers.Add(xNode.ChildNodes.Item(0).InnerText);
                    }
                }
                xml.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;
        }
XmlDocument操作

XmlDocument操作

上一篇:冲破浏览器之潜规则DOCTYPE的各种模式详解


下一篇:【背景建模】SACON