您的位置:首页 > 编程语言 > C#

C#的XML实现记住密码功能

2012-05-25 10:55 344 查看
/// <summary>
        /// 保存用户
        /// </summary>
        public void SetUser()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Application.StartupPath + "//user.xml");
            XmlNodeList userNodes = xmlDoc.SelectSingleNode("root").ChildNodes;
            foreach (XmlNode userNode in userNodes)
            {
                XmlNodeList idandpwd = userNode.ChildNodes;
                foreach (XmlNode idnode in idandpwd)
                {
                    if (idnode.InnerText == cmbUserName.Text)
                    {
                        idnode.NextSibling.InnerText = jnzp.DESEncrypt.Encrypt(txtPwd.Text, "duowanSG");
                        xmlDoc.Save(Application.StartupPath + "//user.xml");
                        return;
                    }
                }
            }

            XmlNode root = xmlDoc.SelectSingleNode("root");//查找<root>
            XmlElement user = xmlDoc.CreateElement("user");
            XmlElement id = xmlDoc.CreateElement("id");
            XmlElement pwd = xmlDoc.CreateElement("pwd");
            id.InnerText = cmbUserName.Text;
            user.AppendChild(id);
            if (checkBox1.Checked)
            {
                pwd.InnerText = jnzp.DESEncrypt.Encrypt(txtPwd.Text, "duowanSG");
            }
            else
            {
                pwd.InnerText = "";
            }
            user.AppendChild(pwd);
            root.AppendChild(user);//添加到<bookstore>节点中
            xmlDoc.Save(Application.StartupPath + "//user.xml");

        }
        /// <summary>
        /// 提取密码
        /// </summary>
        /// <param name="userId">账号</param>
        /// <returns></returns>
        public string GetPwd(string userId)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Application.StartupPath + "//user.xml");
            XmlNodeList userNodes = xmlDoc.SelectSingleNode("root").ChildNodes;
            foreach (XmlNode user in userNodes)
            {
                XmlNodeList idandpwd = user.ChildNodes;
                foreach (XmlNode idnode in idandpwd)
                {
                    if (idnode.InnerText == cmbUserName.Text)
                    {
                        string pwd=idnode.NextSibling.InnerText;
                        if (pwd == "")
                        {
                            return "";
                        }
                        return jnzp.DESEncrypt.Decrypt(pwd, "duowanSG");
                    }
                }
            }
            return "";
        }

==============================XML格式======================
<?xml version="1.0" encoding="utf-8"?>
<root>
  <user>
    <id>abcdefg</id>
    <pwd>FA52C9C83C4ACD73</pwd>
  </user>
</root> 

============================================================
给你说下思路.
当登录时,记录下账号密码,然后向XML中插入节点(不会的话,自己去网上搜一下),首先需要判断当前XML用户是否有了,有的话修改密码就行,没有新建.
在上面程序中,有checkbox来做判断是否记录密码.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: