asp.net 2.0 对XML文件的读写修改操作
2007-03-21 16:15 ruinet 阅读(989) 评论(0) 编辑 收藏 举报xml文件格式如下:
1<?xml version="1.0" encoding="utf-8"?>
2<User>
3 <Person ID="1">
4 <Name>zhangrui</Name>
5 <Age>22</Age>
6 <Sex>男</Sex>
7 <Tel>13411109900</Tel>
8 </Person>
9 <Person ID="2">
10 <Name>王萍</Name>
11 <Age>18</Age>
12 <Sex>女</Sex>
13 <Tel>020412312332</Tel>
14 </Person>
15</User>
2<User>
3 <Person ID="1">
4 <Name>zhangrui</Name>
5 <Age>22</Age>
6 <Sex>男</Sex>
7 <Tel>13411109900</Tel>
8 </Person>
9 <Person ID="2">
10 <Name>王萍</Name>
11 <Age>18</Age>
12 <Sex>女</Sex>
13 <Tel>020412312332</Tel>
14 </Person>
15</User>
A 加载XML文档
1Imports System.Xml
2'定义对象
3Private XmlDoc As XmlDocument
4
5Private Sub LoadXml()
6 XmlDoc = New XmlDataDocument
7 XmlDoc.Load(Server.MapPath("User.xml"))
8 End Sub
2'定义对象
3Private XmlDoc As XmlDocument
4
5Private Sub LoadXml()
6 XmlDoc = New XmlDataDocument
7 XmlDoc.Load(Server.MapPath("User.xml"))
8 End Sub
B 绑定数据到GridView控件
1Private Sub BindData()
2 Dim ds As New DataSet
3 ds.ReadXml(Server.MapPath("User.xml"))
4 GridView1.DataSource = ds.Tables(0)
5 GridView1.DataBind()
6 End Sub
2 Dim ds As New DataSet
3 ds.ReadXml(Server.MapPath("User.xml"))
4 GridView1.DataSource = ds.Tables(0)
5 GridView1.DataBind()
6 End Sub
在数据层中读取xml文档
ds.ReadXml(System.Web.HttpContext.Current.Server.MapPath("../Roles.xml"))
C 向XML文件写入数据 1 Private Sub Add(ByVal Name As String, ByVal Age As Int32, ByVal Sex As String, ByVal Tel As String)
2 LoadXml()
3 Dim Node As XmlNode = XmlDoc.SelectSingleNode("User")
4 Dim Nodes As XmlNodeList = XmlDoc.SelectSingleNode("User").ChildNodes
5 Dim elPerson As XmlElement = XmlDoc.CreateElement("Person")
6
7 Dim elName As XmlElement = XmlDoc.CreateElement("Name")
8 Dim elAge As XmlElement = XmlDoc.CreateElement("Age")
9 Dim elSex As XmlElement = XmlDoc.CreateElement("Sex")
10 Dim elTel As XmlElement = XmlDoc.CreateElement("Tel")
11
12 elPerson.SetAttribute("ID", Nodes.Count + 1)
13
14 elName.InnerText = Name
15 elAge.InnerText = Age
16 elSex.InnerText = Sex
17 elTel.InnerText = Tel
18
19 elPerson.AppendChild(elName)
20 elPerson.AppendChild(elAge)
21 elPerson.AppendChild(elSex)
22 elPerson.AppendChild(elTel)
23
24 Node.AppendChild(elPerson)
25
26 XmlDoc.Save(Server.MapPath("User.xml"))
27
28 End Sub
C 修改XML文档2 LoadXml()
3 Dim Node As XmlNode = XmlDoc.SelectSingleNode("User")
4 Dim Nodes As XmlNodeList = XmlDoc.SelectSingleNode("User").ChildNodes
5 Dim elPerson As XmlElement = XmlDoc.CreateElement("Person")
6
7 Dim elName As XmlElement = XmlDoc.CreateElement("Name")
8 Dim elAge As XmlElement = XmlDoc.CreateElement("Age")
9 Dim elSex As XmlElement = XmlDoc.CreateElement("Sex")
10 Dim elTel As XmlElement = XmlDoc.CreateElement("Tel")
11
12 elPerson.SetAttribute("ID", Nodes.Count + 1)
13
14 elName.InnerText = Name
15 elAge.InnerText = Age
16 elSex.InnerText = Sex
17 elTel.InnerText = Tel
18
19 elPerson.AppendChild(elName)
20 elPerson.AppendChild(elAge)
21 elPerson.AppendChild(elSex)
22 elPerson.AppendChild(elTel)
23
24 Node.AppendChild(elPerson)
25
26 XmlDoc.Save(Server.MapPath("User.xml"))
27
28 End Sub
1Private Sub Edit(ByVal Name As String, ByVal Age As Int32, ByVal Sex As String, ByVal Tel As String)
2 LoadXml()
3 Dim Node As XmlNode = XmlDoc.SelectSingleNode("//Person[Name='" + Name + "']")
4 If Not IsNothing(Node) Then
5 Node.ChildNodes(1).InnerText = Age
6 Node.ChildNodes(2).InnerText = Sex
7 Node.ChildNodes(3).InnerText = Tel
8 End If
9 XmlDoc.Save(Server.MapPath("User.xml"))
10 End Sub
2 LoadXml()
3 Dim Node As XmlNode = XmlDoc.SelectSingleNode("//Person[Name='" + Name + "']")
4 If Not IsNothing(Node) Then
5 Node.ChildNodes(1).InnerText = Age
6 Node.ChildNodes(2).InnerText = Sex
7 Node.ChildNodes(3).InnerText = Tel
8 End If
9 XmlDoc.Save(Server.MapPath("User.xml"))
10 End Sub