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

废话不说,代码如下:XML文件请见 ASP.Net中实现XMl的标识列(ID列)或自增长列
None.gif          private   bool  Insert_XML()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlDocument doc 
= new XmlDocument();
InBlock.gif                
string filename = Server.MapPath(".//XML//User.xml");
InBlock.gif                doc.Load(filename);
InBlock.gif                XmlNode root 
= doc.DocumentElement;
InBlock.gif                XmlNode user 
= doc.CreateElement("user");
InBlock.gif
InBlock.gif                DataSet ds 
= new DataSet();
InBlock.gif                ds.ReadXml(filename); 
InBlock.gif                
int userid = ds.Tables[0].Rows.Count+1;
InBlock.gif                               
//写入userid
InBlock.gif
                XmlElement elem6 = doc.CreateElement("userid");
InBlock.gif                elem6.InnerText 
= userid.ToString();
InBlock.gif                user.AppendChild(elem6);
InBlock.gif                
//写入用户名
InBlock.gif
                XmlElement elem1 = doc.CreateElement("username");
InBlock.gif                elem1.InnerText 
= this.Txt_username.Text.Trim();
InBlock.gif                user.AppendChild(elem1);
InBlock.gif                
//写入密码
InBlock.gif
                XmlElement elem2 = doc.CreateElement("userpass");
InBlock.gif                elem2.InnerText 
= this.Txt_pasw.Text.Trim();
InBlock.gif                user.AppendChild(elem2);
InBlock.gif                
//写入Email
InBlock.gif
                XmlElement elem3 = doc.CreateElement("usermail");
InBlock.gif                elem3.InnerText 
= this.Txt_usermail.Text.Trim();
InBlock.gif                user.AppendChild(elem3);
InBlock.gif                
//写入QQ
InBlock.gif
                XmlElement elem4 = doc.CreateElement("usersite");
InBlock.gif                elem4.InnerText 
= this.Txt_siteurl.Text.Trim();
InBlock.gif                user.AppendChild(elem4);
InBlock.gif                
//写入MSN
InBlock.gif
                XmlElement elem5 = doc.CreateElement("usermsn");
InBlock.gif                elem5.InnerText 
= this.Txt_msn.Text.Trim();
InBlock.gif                user.AppendChild(elem5);
InBlock.gif                
//写入日期
InBlock.gif
                XmlElement elem8 = doc.CreateElement("ndate");
InBlock.gif                elem8.InnerText 
= DateTime.Now.ToString();
InBlock.gif                user.AppendChild(elem8);
InBlock.gif                
InBlock.gif                root.AppendChild(user);
InBlock.gif                doc.Save(filename);    
InBlock.gif                
this.Lab_flag.Text = "注册成功!";
InBlock.gif                
this.HyperLink1.Visible=true;
InBlock.gif                
this.Txt_username.Text="";
InBlock.gif                
this.Txt_pasw.Text="";
InBlock.gif                
this.Txt_firpasw.Text="";
InBlock.gif                
this.Txt_usermail.Text="";
InBlock.gif                
this.Txt_msn.Text="";
InBlock.gif                
this.Txt_siteurl.Text="";
InBlock.gif                
return true;
InBlock.gif               
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.Lab_flag.Text = ex.Message;
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/tenghoo/archive/2006/10/11/525965.html

你可能感兴趣的:(ASP.Net 向xml文件中添加数据(insert操作))