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;
            }

        }
posted @ 2006-10-11 10:32  青羽  阅读(1554)  评论(1编辑  收藏  举报