.net 操作xml保存用户信息

<?xml version="1.0" encoding="utf-8" ?>
// xml 文件  存储用户注册信息  暂时代替用户表
<UserInfo>
    <User>

        <name>admin</name>
        <pwd>888888</pwd>
        <age>22</age>
        <sex>男</sex>
    </User>


</UserInfo>

 

 

 

 

   //写入xml
       public void InsertXML(User model)
       {
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(xmlPath);

       //获取根节点
           XmlNode root = xmlDoc.SelectSingleNode("UserInfo");
           XmlElement xment = xmlDoc.CreateElement("User");

    //创建节点
           XmlElement xmlname = xmlDoc.CreateElement("name");
           xmlname.InnerText = model.UserName;
           xment.AppendChild(xmlname);


           XmlElement xmlpwd = xmlDoc.CreateElement("pwd");
           xmlpwd.InnerText = model.Pwd;
           xment.AppendChild(xmlpwd);

           XmlElement xmlage = xmlDoc.CreateElement("age");
           xmlage.InnerText = model.Age;
           xment.AppendChild(xmlage);

           XmlElement xmlsex = xmlDoc.CreateElement("sex");
           xmlsex.InnerText = model.Sex;
           xment.AppendChild(xmlsex);

           root.AppendChild(xment);
           string strEx = xmlPath.Substring(6,xmlPath.Length-6);
           xmlDoc.Save(strEx);
          
       }

       //登录
       public bool IsLogin(User model)
       {
           List<string> list  = new List<string>();
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(xmlPath);

           XmlNode xmlNode = xmlDoc.SelectSingleNode("UserInfo");
           bool ft = false;
           foreach (XmlNode node in xmlNode)
           {

      //验证是否登录通过
               if (model.UserName.ToLower() == node.ChildNodes[0].InnerText.ToLower() && model.Pwd.ToLower()==node.ChildNodes[1].InnerText.ToLower())
               {
                   ft = true;
                   break;
               }
           }

           return ft;
       }



       //读取图像

       public List<string> LoadImg()
       {
           List<string> list = new List<string>();
           XmlDocument xmlDoc = new XmlDocument();
           xmlDoc.Load(xmlImgPath);
           XmlElement xmlEle = xmlDoc.DocumentElement;
         

           XmlNodeList xmlList = xmlEle.SelectNodes("Image/Items/Item");
           foreach (XmlNode node in xmlList)
           {
               list.Add(node.Attributes["src"].InnerXml.ToString());
           }
           return list;
       }

转载于:https://www.cnblogs.com/syyblog/archive/2011/01/30/1947969.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值