使用XmlDocument类完成对XML的查、删、添、改



 1<%@ Page language="c#" Codebehind="xdoc.aspx.cs" AutoEventWireup="false" Inherits="XML.xdoc" %>
 2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
 3<HTML>
 4    <HEAD>
 5        <title>xdoc</title>
 6        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 7        <meta content="C#" name="CODE_LANGUAGE">
 8        <meta content="JavaScript" name="vs_defaultClientScript">
 9        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
10    </HEAD>
11    <body MS_POSITIONING="GridLayout">
12        <form id="Form1" method="post" runat="server">
13            <FONT face="宋体">
14                <asp:datagrid id="dg" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 56px" runat="server"
15                    ForeColor="Black" BorderStyle="None" BorderWidth="1px" BorderColor="#DEDFDE" BackColor="White"
16                    CellPadding="4" GridLines="Vertical" AutoGenerateColumns="False" Width="432px">
17                    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CE5D5A"></SelectedItemStyle>
18                    <AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
19                    <ItemStyle BackColor="#F7F7DE"></ItemStyle>
20                    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6B696B"></HeaderStyle>
21                    <FooterStyle BackColor="#CCCC99"></FooterStyle>
22                    <Columns>
23                        <asp:BoundColumn DataField="name" HeaderText="名字"></asp:BoundColumn>
24                        <asp:BoundColumn DataField="City" HeaderText="城市"></asp:BoundColumn>
25                        <asp:BoundColumn DataField="Email" HeaderText="邮件地址"></asp:BoundColumn>
26                        <asp:BoundColumn DataField="Time" HeaderText="时间"></asp:BoundColumn>
27                    </Columns>
28                    <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
29                </asp:datagrid><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 112px; POSITION: absolute; TOP: 8px" runat="server"
30                    Width="336px" BorderWidth="2px" BorderStyle="Ridge" Font-Size="X-Large" Font-Bold="True" ForeColor="Red">XmlDocument类的挖掘</asp:label><asp:dropdownlist id="ddl" style="Z-INDEX: 103; LEFT: 128px; POSITION: absolute; TOP: 280px" runat="server"></asp:dropdownlist><asp:label id="Label2" style="Z-INDEX: 104; LEFT: 64px; POSITION: absolute; TOP: 280px" runat="server">姓名:</asp:label><asp:button id="Button1" style="Z-INDEX: 105; LEFT: 64px; POSITION: absolute; TOP: 328px" runat="server"
31                    Width="64px" Text="查询"></asp:button><asp:label id="Label3" style="Z-INDEX: 106; LEFT: 64px; POSITION: absolute; TOP: 384px" runat="server">邮件地址:</asp:label><asp:button id="Button2" style="Z-INDEX: 107; LEFT: 152px; POSITION: absolute; TOP: 328px" runat="server"
32                    Width="64px" Text="删除"></asp:button><asp:button id="Button3" style="Z-INDEX: 108; LEFT: 432px; POSITION: absolute; TOP: 280px" runat="server"
33                    Width="56px" Text="添加" Height="80px"></asp:button><asp:textbox id="tbn" style="Z-INDEX: 109; LEFT: 152px; POSITION: absolute; TOP: 384px" runat="server"
34                    Width="328px"></asp:textbox><asp:button id="Button4" style="Z-INDEX: 110; LEFT: 64px; POSITION: absolute; TOP: 416px" runat="server"
35                    Width="424px" Text="修改邮件地址"></asp:button>
36                <asp:TextBox id="tbna" style="Z-INDEX: 111; LEFT: 312px; POSITION: absolute; TOP: 280px" runat="server"
37                    Width="104px"></asp:TextBox>
38                <asp:TextBox id="tbc" style="Z-INDEX: 112; LEFT: 312px; POSITION: absolute; TOP: 312px" runat="server"
39                    Width="104px" Height="24px"></asp:TextBox>
40                <asp:TextBox id="tbe" style="Z-INDEX: 113; LEFT: 312px; POSITION: absolute; TOP: 344px" runat="server"
41                    Width="104px" Height="24px"></asp:TextBox>
42                <asp:Label id="Label4" style="Z-INDEX: 114; LEFT: 264px; POSITION: absolute; TOP: 280px" runat="server">姓名:</asp:Label>
43                <asp:Label id="Label5" style="Z-INDEX: 115; LEFT: 264px; POSITION: absolute; TOP: 312px" runat="server">城市:</asp:Label>
44                <asp:Label id="Label6" style="Z-INDEX: 116; LEFT: 264px; POSITION: absolute; TOP: 344px" runat="server">Email:</asp:Label></FONT></form>
45    </body>
46</HTML>
47


  1using System;
  2using System.Collections;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Web;
  7using System.Web.SessionState;
  8using System.Web.UI;
  9using System.Web.UI.WebControls;
 10using System.Web.UI.HtmlControls;
 11//新引入两个命名空间
 12using System.Xml;
 13using System.Xml.XPath;
 14
 15namespace XML
 16{
 17    /// <summary>
 18    /// xdoc 的摘要说明。
 19    /// </summary>

 20    public class xdoc : System.Web.UI.Page
 21    {
 22        protected System.Web.UI.WebControls.Label Label1;
 23        protected System.Web.UI.WebControls.Label Label2;
 24        protected System.Web.UI.WebControls.Button Button1;
 25        protected System.Web.UI.WebControls.Label Label3;
 26        protected System.Web.UI.WebControls.Button Button2;
 27        protected System.Web.UI.WebControls.Button Button3;
 28        protected System.Web.UI.WebControls.DataGrid dg;
 29        protected System.Web.UI.WebControls.DropDownList ddl;
 30        protected System.Web.UI.WebControls.TextBox tbn;
 31        protected System.Web.UI.WebControls.Label Label4;
 32        protected System.Web.UI.WebControls.Label Label5;
 33        protected System.Web.UI.WebControls.Label Label6;
 34        protected System.Web.UI.WebControls.TextBox tbna;
 35        protected System.Web.UI.WebControls.TextBox tbc;
 36        protected System.Web.UI.WebControls.TextBox tbe;
 37        protected System.Web.UI.WebControls.Button Button4;
 38    
 39        private void Page_Load(object sender, System.EventArgs e)
 40        {//看动作名字就知道了是载入了
 41            if(!Page.IsPostBack)
 42            {
 43                fill();
 44            }

 45        }

 46
 47        Web 窗体设计器生成的代码
 71
 72        private void fill()
 73        {//绑定方法
 74            DataSet ds=new DataSet();
 75            ds.ReadXml(Server.MapPath("\\xml\\xml\\doc.xml"));
 76            dg.DataSource=ds.Tables[0];
 77            dg.DataBind();
 78            XmlDocument xd=new XmlDocument();
 79            xd.Load(Server.MapPath("\\xml\\xml\\doc.xml"));
 80            System.Xml.XmlNodeList xnl=xd.GetElementsByTagName("Name");
 81            ddl.Items.Clear();
 82            for(int i=0;i<xnl.Count;i++)
 83            {
 84                ddl.Items.Add(xnl[i].InnerText);
 85            }

 86        }

 87
 88        private void Button1_Click(object sender, System.EventArgs e)
 89        {//查询动作
 90            XmlDocument xd=new XmlDocument();
 91            xd.Load(Server.MapPath("\\xml\\xml\\doc.xml"));
 92            tbn.Text=xd.SelectSingleNode("//User[Name='"+ddl.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText.ToString();
 93
 94        }

 95
 96        private void Button2_Click(object sender, System.EventArgs e)
 97        {//删除动作
 98            XmlDocument xdoc=new XmlDocument();
 99            xdoc.Load(Server.MapPath("\\xml\\xml\\doc.xml"));
100            XmlNodeList xnl=xdoc.SelectSingleNode("dbGuest").ChildNodes;
101            
102            foreach(XmlNode xn in xnl)
103            {
104                XmlElement xe=(XmlElement)xn;
105                XmlNodeList node=xe.GetElementsByTagName("Name");//查找他的Name行
106                if(node.Count>0)
107                {
108                    if(node[0].InnerText==ddl.SelectedItem.Text)//如果当前节点的名字和下拉列表传来的一样,就删除
109                    {
110                        xe.RemoveAll();//删除节点的所有指定属性和子集,但是不删除默认属性,我很郁闷,不知道怎么连属性一起删除,如果有朋友知道,请一定告诉我下,谢谢了!
111
112                    }

113                    break;
114                }

115
116            }

117            xdoc.Save(Server.MapPath("\\xml\\xml\\doc.xml"));
118            fill();
119        }

120
121        private void Button3_Click(object sender, System.EventArgs e)
122        {//添加动作
123            string name=this.tbna.Text.ToString();
124            string city=this.tbc.Text.ToString();
125            string email=this.tbe.Text.ToString();
126            
127            XmlDocument xd=new XmlDocument();
128            xd.Load(Server.MapPath("\\xml\\xml\\doc.xml"));
129            //查找dbGuest节点
130            XmlNode xn=xd.SelectSingleNode("dbGuest");
131            //创建user节点
132            XmlElement xe_u=xd.CreateElement("User");
133            //创建一个name元素
134            XmlElement xe_a1=xd.CreateElement("Name");
135            xe_a1.InnerText=name;//设置该元素的值
136            xe_u.AppendChild(xe_a1);//把刚才创建的元素插入name节点里面去
137            XmlElement xe_a2=xd.CreateElement("City");
138            xe_a2.InnerText=city;
139            xe_u.AppendChild(xe_a2);
140            XmlElement xe_a3=xd.CreateElement("Email");
141            xe_a3.InnerText=email;
142            xe_u.AppendChild(xe_a3);
143            XmlElement xe_a4=xd.CreateElement("Time");
144            xe_a4.InnerText=(DateTime.Now).ToString();
145            xe_u.AppendChild(xe_a4);
146            //将User节点插入到dbGuest
147            xn.AppendChild(xe_u);
148            //保存XML文件
149            xd.Save(Server.MapPath("\\xml\\xml\\doc.xml"));
150            fill();
151        }

152
153        private void Button4_Click(object sender, System.EventArgs e)
154        {//修改邮件地址动作
155            XmlDocument xd=new XmlDocument();
156            xd.Load(Server.MapPath("\\xml\\xml\\doc.xml"));//载入XML文件
157            XmlNodeList xnl=xd.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的全部子节点
158            foreach(XmlNode xn in xnl)//迭代所有子节点
159            {
160                XmlElement xe=(XmlElement)xn;//将迭代到的子节点转换成XmlElement类型
161                XmlNodeList xnl2=xe.GetElementsByTagName("Name");//返回所有Name列的数据
162                if(xnl2.Count>0)//如果大于零就表示有撒,
163                {
164                    if(xnl2[0].InnerText==ddl.SelectedItem.Text)//如果该列有和被下拉列表中返回的值一样的数据的话
165                    {
166                        XmlNodeList xnl3=xe.ChildNodes;//再获取下面所有的子节点
167                        foreach(XmlNode xn1 in xnl3)//再迭代子集
168                        {
169                            XmlElement xe2=(XmlElement)xn1;//转换类型
170                            if(xe2.Name=="Email")//如果节点名字是Email的话
171                            {
172                                xe2.InnerText=tbn.Text;//该节点的值就替换成文本框中的
173                                break;
174                            }

175                        }

176                        break;
177                    }

178                }

179            }

180            xd.Save(Server.MapPath("\\xml\\xml\\doc.xml"));
181            fill();
182        }

183    }

184}

185


 1<?xml version="1.0" encoding="utf-8"?>
 2<dbGuest>
 3  <User>
 4    <Name>天轰穿</Name>
 5    <City>江油</City>
 6    <Email>[email protected]</Email>
 7    <Time>2006-5-6 16:10:56</Time>
 8  </User>
 9  <User>
10    <Name>田洪川</Name>
11    <City>绵阳</City>
12    <Email>[email protected]</Email>
13    <Time>2006-5-6 16:06:51</Time>
14  </User>
15  <User>
16    <Name>黄小梅</Name>
17    <City>南充</City>
18    <Email>[email protected]</Email>
19    <Time>2006-5-6 16:07:15</Time>
20  </User>
21</dbGuest>

你可能感兴趣的:(document)