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

/// <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来做判断是否记录密码.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值