• ASP.Net 向xml文件中添加数据(insert操作)


    废话不说,代码如下:XML文件请见ASP.Net中实现XMl的标识列(ID列)或自增长列
            private bool Insert_XML()
            
    {
                
    try
                
    {
                    XmlDocument doc 
    = new XmlDocument();
                    
    string filename = Server.MapPath(".//XML//User.xml");
                    doc.Load(filename);
                    XmlNode root 
    = doc.DocumentElement;
                    XmlNode user 
    = doc.CreateElement("user");

                    DataSet ds 
    = new DataSet();
                    ds.ReadXml(filename); 
                    
    int userid = ds.Tables[0].Rows.Count+1;
                                   
    //写入userid
                    XmlElement elem6 = doc.CreateElement("userid");
                    elem6.InnerText 
    = userid.ToString();
                    user.AppendChild(elem6);
                    
    //写入用户名
                    XmlElement elem1 = doc.CreateElement("username");
                    elem1.InnerText 
    = this.Txt_username.Text.Trim();
                    user.AppendChild(elem1);
                    
    //写入密码
                    XmlElement elem2 = doc.CreateElement("userpass");
                    elem2.InnerText 
    = this.Txt_pasw.Text.Trim();
                    user.AppendChild(elem2);
                    
    //写入Email
                    XmlElement elem3 = doc.CreateElement("usermail");
                    elem3.InnerText 
    = this.Txt_usermail.Text.Trim();
                    user.AppendChild(elem3);
                    
    //写入QQ
                    XmlElement elem4 = doc.CreateElement("usersite");
                    elem4.InnerText 
    = this.Txt_siteurl.Text.Trim();
                    user.AppendChild(elem4);
                    
    //写入MSN
                    XmlElement elem5 = doc.CreateElement("usermsn");
                    elem5.InnerText 
    = this.Txt_msn.Text.Trim();
                    user.AppendChild(elem5);
                    
    //写入日期
                    XmlElement elem8 = doc.CreateElement("ndate");
                    elem8.InnerText 
    = DateTime.Now.ToString();
                    user.AppendChild(elem8);
                    
                    root.AppendChild(user);
                    doc.Save(filename);    
                    
    this.Lab_flag.Text = "注册成功!";
                    
    this.HyperLink1.Visible=true;
                    
    this.Txt_username.Text="";
                    
    this.Txt_pasw.Text="";
                    
    this.Txt_firpasw.Text="";
                    
    this.Txt_usermail.Text="";
                    
    this.Txt_msn.Text="";
                    
    this.Txt_siteurl.Text="";
                    
    return true;
                   
                }

                
    catch(Exception ex)
                
    {
                    
    this.Lab_flag.Text = ex.Message;
                    
    return false;
                }

            }
    作者:青羽
  • 相关阅读:
    程序员都必读
    ia-64 vs x86-64
    Linux内核学习
    开源liscense对比
    列存储
    大数据科普
    [USACO1.5]数字三角形
    [USACO08FEB]酒店Hotel
    数的划分
    CodeForce 18D
  • 原文地址:https://www.cnblogs.com/tenghoo/p/525965.html
Copyright © 2020-2023  润新知