保存用户名和密码(c#)

using System;
using System.Collections.Generic;
using System.Text;

namespace xml_operator
{
    public class UserAndPass
    {
        private string fileName;
        private string rootName;
        private string userNode;
        private string userValue;
        private string passNode;
        private string passValue;

        #region method UserAndPass
        /// <summary>
        /// xml文档构造
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="RootName"></param>
        /// <param name="UserNode"></param>
        /// <param name="UserValue"></param>
        /// <param name="PassNode"></param>
        /// <param name="PassValue"></param>
        public UserAndPass(string FileName, string RootName, string UserNode, string UserValue, string PassNode, string PassValue)
        {
            this.fileName = FileName;
            this.rootName = RootName;
            this.userNode = UserNode;
            this.userValue = UserValue;
            this.passNode = PassNode;
            this.passValue = PassNode;
        }
        #endregion

        #region method CreateXml
        /// <summary>
        /// 创建xml文档
        /// </summary>
        public void CreateXml()
        {
            XmlTextWriter textWriter = new XmlTextWriter(System.Environment.CurrentDirectory + "//" + this.fileName, null);
            textWriter.Formatting = Formatting.Indented;
            // 开始写过程,调用WriteStartDocument方法
            textWriter.WriteStartDocument();

            // 写入说明
            textWriter.WriteComment("use the xml for the username and password");

            //创建一个节点
            textWriter.WriteStartElement(this.rootName);
            textWriter.WriteElementString(this.userNode, this.userValue);
            textWriter.WriteElementString(this.passNode, this.passValue);
            textWriter.WriteEndElement();

 

            // 写文档结束,调用WriteEndDocument方法
            textWriter.WriteEndDocument();

            // 关闭textWriter
            textWriter.Close();
        }
        #endregion

        #region method UpdateXml
        /// <summary>
        /// 更新xml文档节点关联值
        /// </summary>
        public void UpdateXml()
        {
            XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
            xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
            foreach (XmlNode xNode in nodeList)//遍历所有子节点
            {
                XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
                if (xElement.Name == this.userNode)
                {
                    xElement.InnerText = this.userValue;
                }

                if (xElement.Name == this.passNode)
                {
                    xElement.InnerText = this.passValue;
                }
            }
            xmlDoc.Save(System.Environment.CurrentDirectory + "//"+this.fileName);//保存修改
        }
        #endregion

        #region method GetNameValue
        /// <summary>
        /// 获得Name节点的关联值
        /// </summary>
        /// <returns></returns>
        public string GetNameValue()
        {
            XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
            xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
            foreach (XmlNode xNode in nodeList)//遍历所有子节点
            {
                XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
                if (xElement.Name == this.userNode)
                {
                    return xElement.InnerText;
                }
            }
        }
        #endregion

        #region method GetPassValue
        /// <summary>
        /// 获得Pass节点的关联值
        /// </summary>
        /// <returns></returns>
        public stirng GetPassValue()
        {
            XmlDocument xmlDoc = new XmlDocument();//修改添加的一行
            xmlDoc.Load(System.Environment.CurrentDirectory + "//" + this.fileName);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(this.rootName).ChildNodes;//获取bookstore节点的所有子节点
            foreach (XmlNode xNode in nodeList)//遍历所有子节点
            {
                XmlElement xElement = (XmlElement)xNode;//将子节点类型转换为XmlElement类型
                if (xElement.Name == this.passNode)
                {
                    return xElement.InnerText;
                }
            }
        }
        #endregion
    }
}

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值