C#(Asp.net)对XML文件增、删、改、查(基于DOM)

    技术2022-05-11  66

    asp.net文件xmlMain.aspx 和people.xml文件在同一目录下

    前台html文件

     

    <% @ Page language="c#" Codebehind="xmlMain.aspx.cs" AutoEventWireup="false" Inherits="xmlDocWeb.WebForm1"  %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  > < HTML >      < HEAD >          < title > WebForm1 </ title >          < meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >          < meta  name ="CODE_LANGUAGE"  Content ="C#" >          < meta  name ="vs_defaultClientScript"  content ="JavaScript" >          < meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >      </ HEAD >      < body  MS_POSITIONING ="GridLayout"  style ="FONT-SIZE:10pt" >          < form  id ="Form1"  method ="post"  runat ="server" >              < FONT  face ="宋体" >                  < asp:DataGrid  id ="grd"  style ="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 40px"  runat ="server"                     Width ="456px" ></ asp:DataGrid >                  < asp:Button  id ="btnAttributes"  style ="Z-INDEX: 122; LEFT: 352px; POSITION: absolute; TOP: 448px"                     runat ="server"  Text ="遍历属性" ></ asp:Button >                  < asp:Button  id ="Button1"  style ="Z-INDEX: 121; LEFT: 80px; POSITION: absolute; TOP: 424px"  tabIndex ="6"                     runat ="server"  Text ="修改" ></ asp:Button >                  < asp:TextBox  id ="txttel2"  style ="Z-INDEX: 120; LEFT: 80px; POSITION: absolute; TOP: 384px"  tabIndex ="5"                     runat ="server" ></ asp:TextBox >                  < asp:Label  id ="Label7"  style ="Z-INDEX: 119; LEFT: 80px; POSITION: absolute; TOP: 360px"  runat ="server" > 修改用户电话 </ asp:Label >                  < asp:Button  id ="btnAdd"  style ="Z-INDEX: 118; LEFT: 352px; POSITION: absolute; TOP: 408px"  runat ="server"                     Text ="添加" ></ asp:Button >                  < asp:TextBox  id ="txtemail"  style ="Z-INDEX: 117; LEFT: 384px; POSITION: absolute; TOP: 368px"                     runat ="server"  Width ="136px"  tabIndex ="11" ></ asp:TextBox >                  < asp:TextBox  id ="txttel"  style ="Z-INDEX: 116; LEFT: 384px; POSITION: absolute; TOP: 336px"  runat ="server"                     Width ="136px"  tabIndex ="10" ></ asp:TextBox >                  < asp:TextBox  id ="txtaddress"  style ="Z-INDEX: 115; LEFT: 384px; POSITION: absolute; TOP: 304px"                     runat ="server"  Width ="136px"  tabIndex ="9" ></ asp:TextBox >                  < asp:TextBox  id ="txtid"  style ="Z-INDEX: 114; LEFT: 384px; POSITION: absolute; TOP: 280px"  runat ="server"                     Width ="136px"  tabIndex ="8" ></ asp:TextBox >                  < asp:Label  id ="Label6"  style ="Z-INDEX: 113; LEFT: 328px; POSITION: absolute; TOP: 368px"  runat ="server" > Email </ asp:Label >                  < asp:Label  id ="Label5"  style ="Z-INDEX: 112; LEFT: 328px; POSITION: absolute; TOP: 344px"  runat ="server" > TEL </ asp:Label >                  < asp:Label  id ="Label4"  style ="Z-INDEX: 111; LEFT: 320px; POSITION: absolute; TOP: 312px"  runat ="server" > ADDRESS </ asp:Label >                  < asp:Label  id ="Label3"  style ="Z-INDEX: 110; LEFT: 320px; POSITION: absolute; TOP: 280px"  runat ="server" > ID </ asp:Label >                  < asp:TextBox  id ="txtName2"  style ="Z-INDEX: 109; LEFT: 384px; POSITION: absolute; TOP: 248px"                     runat ="server"  Width ="136px"  tabIndex ="7" ></ asp:TextBox >                  < asp:Label  id ="Label2"  style ="Z-INDEX: 108; LEFT: 320px; POSITION: absolute; TOP: 248px"  runat ="server" > Name </ asp:Label >                  < asp:Label  id ="Label1"  style ="Z-INDEX: 107; LEFT: 320px; POSITION: absolute; TOP: 216px"  runat ="server" > 添加用户信息 </ asp:Label >                  < asp:TextBox  id ="txtname"  style ="Z-INDEX: 106; LEFT: 80px; POSITION: absolute; TOP: 280px"  runat ="server"                     Width ="120px"  tabIndex ="3" ></ asp:TextBox >                  < asp:Button  id ="btndel"  style ="Z-INDEX: 105; LEFT: 80px; POSITION: absolute; TOP: 312px"  runat ="server"                     Text ="删除"  tabIndex ="4" ></ asp:Button >                  < asp:Label  id ="lblTel"  style ="Z-INDEX: 104; LEFT: 192px; POSITION: absolute; TOP: 224px"  runat ="server" > Label </ asp:Label >                  < asp:Button  id ="btnQuery"  style ="Z-INDEX: 103; LEFT: 80px; POSITION: absolute; TOP: 248px"  runat ="server"                     Text ="查询"  tabIndex ="2" ></ asp:Button >                  < asp:DropDownList  id ="ddlName"  style ="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 216px"  runat ="server"                     Width ="95px"  Height ="78px"  tabIndex ="1" ></ asp:DropDownList ></ FONT >          </ form >      </ body > </ HTML >

     后台C#文件

     

    using  System; using  System.Collections; using  System.ComponentModel; using  System.Data; using  System.Drawing; using  System.Web; using  System.Web.SessionState; using  System.Web.UI; using  System.Web.UI.WebControls; using  System.Web.UI.HtmlControls; using  System.Xml; using  System.Xml.XPath; using  System.Text; namespace  xmlDocWeb {    /// <summary>    /// WebForm1 的摘要说明。    /// </summary>    /// 把xml传到客户端处理,充分利用客户端的资源    public class WebForm1 : System.Web.UI.Page    {        protected System.Web.UI.WebControls.DropDownList ddlName;        protected System.Web.UI.WebControls.Button btnQuery;        protected System.Web.UI.WebControls.Label lblTel;        protected System.Web.UI.WebControls.Button btndel;        protected System.Web.UI.WebControls.TextBox txtname;        protected System.Web.UI.WebControls.Label Label1;        protected System.Web.UI.WebControls.Label Label2;        protected System.Web.UI.WebControls.TextBox txtName2;        protected System.Web.UI.WebControls.Label Label3;        protected System.Web.UI.WebControls.Label Label4;        protected System.Web.UI.WebControls.Label Label5;        protected System.Web.UI.WebControls.Label Label6;        protected System.Web.UI.WebControls.TextBox txtid;        protected System.Web.UI.WebControls.TextBox txtaddress;        protected System.Web.UI.WebControls.TextBox txttel;        protected System.Web.UI.WebControls.TextBox txtemail;        protected System.Web.UI.WebControls.Button btnAdd;        protected System.Web.UI.WebControls.Label Label7;        protected System.Web.UI.WebControls.TextBox txttel2;        protected System.Web.UI.WebControls.Button Button1;        protected System.Web.UI.WebControls.Button btnAttributes;        protected System.Web.UI.WebControls.DataGrid grd;        StringBuilder str;        private void Page_Load(object sender, System.EventArgs e)        {            if(!IsPostBack)            {                this.Bind();            }            //this.btndel.Attributes.Add("onclick","javasrcipt:return confirm('delete?');");        }        Web 窗体设计器生成的代码        private void Bind()        {            DataSet ds = new DataSet();            ds.ReadXml(Server.MapPath("people.xml"));            grd.DataSource = ds.Tables[0].DefaultView;            grd.DataBind();            XmlDocument doc = new XmlDocument();                        doc.Load(Server.MapPath("people.xml"));            XmlNodeList elemlist = doc.GetElementsByTagName("Name");            this.ddlName.Items.Clear();            for(int i = 0; i<elemlist.Count;i++)            {                this.ddlName.Items.Add(elemlist[i].InnerXml);            }            //Response.Write(doc.DocumentElement.SelectSingleNode("person[Name='accp']/Tel").InnerXml);        }        private void btnQuery_Click(object sender, System.EventArgs e)        {            XmlDocument doc = new XmlDocument();            doc.Load(Server.MapPath("people.xml"));                        if(doc.SelectSingleNode("//person[Name='"+this.ddlName.SelectedItem.Text+"' ]/Tel"!= null)            {                this.lblTel.Text = doc.SelectSingleNode("//person[Name='"+this.ddlName.SelectedItem.Text+"' ]/Tel").InnerXml;            }            else            {                this.lblTel.Text = "没有联系电话";            }        }        private void btndel_Click(object sender, System.EventArgs e)        {            XmlDocument xmlDoc = new XmlDocument();            xmlDoc.Load(Server.MapPath("people.xml"));//            XmlNodeList xnlist = xmlDoc.SelectSingleNode("people").ChildNodes;//            //            foreach(XmlNode xnode in xnlist)//            {//                XmlElement xe = (XmlElement)xnode;//                XmlNodeList nodes = xe.GetElementsByTagName("Name");//                if(nodes.Count > 0 )//                {//                    if(nodes[0].InnerText == this.ddlName.SelectedItem.Text)                    //                        xe.RemoveAll();                        //                        break;//                }//            }                        //XmlNode node = xmlDoc.DocumentElement.SelectSingleNode("person[Name='"+this.ddlName.SelectedItem.Text+"' ]");            if(this.txtname.Text=="")            {                            Response.Write("<script language='javascript'>alert('请输入.....!');</script>");                            }            else            {                            XmlNode node = xmlDoc.DocumentElement.SelectSingleNode("person[Name='"+this.txtname.Text+"' ]");                if(node != null)                {                    xmlDoc.DocumentElement.RemoveChild(node);                    xmlDoc.Save(Server.MapPath("people.xml"));                    this.Bind();                }                else                {                    Response.Write("<script language='javascript'>alert('此用户不存在!');</script>");                }            }        }        private void btnAdd_Click(object sender, System.EventArgs e)        {            XmlDocument xmldoc = new XmlDocument();            xmldoc.Load(Server.MapPath("people.xml"));            XmlNodeList xnlist = xmldoc.SelectSingleNode("people").ChildNodes;            for(int i = 0; i < xnlist.Count; i++)            {                XmlNode node = xmldoc.DocumentElement.SelectNodes("//person/@id")[i];                if(this.txtid.Text==node.InnerText)                {                    Response.Write("<script language='javascript'>alert('此ID号已存在!');</script>");                    return;                }                            }            //----创建元素------            XmlElement name = xmldoc.CreateElement("Name");            XmlElement address = xmldoc.CreateElement("Address");            XmlElement tel = xmldoc.CreateElement("Tel");            XmlElement email = xmldoc.CreateElement("Email");            XmlElement person = xmldoc.CreateElement("person");                        //----赋值------            name.InnerText = this.txtName2.Text.Trim();            address.InnerText = this.txtaddress.Text.Trim();            tel.InnerText = this.txttel.Text.Trim();            email.InnerText = this.txtemail.Text.Trim();            person.SetAttribute("id",this.txtid.Text.Trim());            person.AppendChild(name);            person.AppendChild(address);            person.AppendChild(tel);            person.AppendChild(email);            xmldoc.DocumentElement.AppendChild(person);            xmldoc.Save(Server.MapPath("people.xml"));            this.Bind();                }        private void Button1_Click(object sender, System.EventArgs e)        {            XmlDocument xmldoc = new XmlDocument();            xmldoc.Load(Server.MapPath("people.xml"));            XmlNode node = xmldoc.DocumentElement.SelectSingleNode("person[Name='"+ this.ddlName.SelectedItem.Text + "']/Tel");            node.InnerText = this.txttel2.Text.Trim();            xmldoc.Save(Server.MapPath("people.xml"));            this.Bind();        }        private void btnAttributes_Click(object sender, System.EventArgs e)        {            XmlDocument xmldoc = new XmlDocument();            xmldoc.Load(Server.MapPath("people.xml"));            XmlElement root = xmldoc.DocumentElement;                            for(int i = 0; i<root.ChildNodes.Count;i++)            {                            str = new StringBuilder();                str.Append(root.ChildNodes[i].Attributes[0].InnerText);                //Response.Write(str);                //Response.Write("<script language = 'javascript'>alert("+str+");</script>");            }                        Response.Write(str);                            }                    }}

     

    XML文件

     

    <? xml version="1.0" encoding="gb2312" ?> < people >    < person  id ="e09"  city ="xuzhou" >      < Name > ljy </ Name >      < Address > jangsu dafeng </ Address >      < Tel > 138 </ Tel >      < Email > ljy@126.com </ Email >    </ person >    < person  id ="e10"  city ="nanjing" >      < Name > accp </ Name >      < Address > xuzhou </ Address >      < Tel > 9999 </ Tel >      < Email > accp@126.com </ Email >    </ person >    < person  id ="e11" >      < Name > jack </ Name >      < Address > jiangsu najing </ Address >      < Tel > 025-987 </ Tel >      < Email > jack@126.com </ Email >    </ person >    < person  id ="12" >      < Name > xxxuuu </ Name >      < Address > jiang su dafeng </ Address >      < Tel > 123 </ Tel >      < Email > 123@yahoo.com </ Email >    </ person > </ people >

    最新回复(0)