Add four buttons to web page, Add, modify, Delete and Clear
Add 2 functions: One is loadXmlData, and the other is FindXmlData
Add 2 functions: One is loadXmlData, and the other is FindXmlData
//insert data to xml file Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(Server.MapPath("App_Data\smallfools.xml")) Dim newelement As XmlElement = xmldoc.CreateElement("poems") Dim xmlAuthor As XmlElement = xmldoc.CreateElement("author") Dim xmlTitle As XmlElement = xmldoc.CreateElement("title") Dim xmlContent As XmlElement = xmldoc.CreateElement("content") xmlAuthor.InnerText = Me.TextBox1.Text.Trim() xmlTitle.InnerText = Me.TextBox2.Text.Trim() xmlContent.InnerText = Me.TextBox3.Text.Trim() newelement.AppendChild(xmlAuthor) newelement.AppendChild(xmlTitle) newelement.AppendChild(xmlContent) xmldoc.DocumentElement.AppendChild(newelement) xmldoc.Save(Server.MapPath("App_Data\smallfools.xml")) loadXmlData() End Sub //modify one xml data base on selecteditem Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(Server.MapPath("App_Data\smallfools.xml")) If Int32.Parse(Me.lblSelectIndex.Text) = -1 Then Me.RegisterClientScriptBlock("alertmessage", "<script>alert('please select one modify data item.')</script>") Else Dim xmlnode As XmlNode = xmldoc.DocumentElement.ChildNodes.Item(Int32.Parse(Me.lblSelectIndex.Text)) xmlnode("author").InnerText = Me.TextBox1.Text.Trim() xmlnode("title").InnerText = Me.TextBox2.Text.Trim() xmlnode("content").InnerText = Me.TextBox3.Text.Trim() xmldoc.Save(Server.MapPath("App_Data\smallfools.xml")) End If loadXmlData() End Sub //Delete one xml data base on selecteditem Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(Server.MapPath("App_Data\smallfools.xml")) Dim xmlnode As XmlNode = xmldoc.DocumentElement.ChildNodes.Item(Int32.Parse(Me.lblSelectIndex.Text)) xmlnode.ParentNode.RemoveChild(xmlnode) xmldoc.Save(Server.MapPath("App_Data\smallfools.xml")) loadXmlData() Me.TextBox1.Text = "" Me.TextBox2.Text = "" Me.TextBox3.Text = "" End Sub //clear textbox value Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click Me.TextBox1.Text = "" Me.TextBox2.Text = "" Me.TextBox3.Text = "" End Sub //load xml data from xml file Private Sub loadXmlData() Dim myDs As DataSet = New DataSet() myDs.ReadXml(Server.MapPath("App_Data\smallfools.xml")) If myDs.Tables.Count <> 0 Then Me.GridView1.DataSource = myDs Me.GridView1.DataBind() End If End Sub //select one data and fill data to textbox Private Sub FindXmlData(ByVal selectedIndex As Integer) Dim xmldoc As XmlDocument = New XmlDocument() xmldoc.Load(Server.MapPath("App_Data\smallfools.xml")) Dim xmlnodelist As XmlNodeList = xmldoc.DocumentElement.ChildNodes Dim xmlnode As XmlNode = xmlnodelist.Item(selectedIndex) Me.textBox1.Text = xmlnode("author").InnerText Me.textBox2.Text = xmlnode("title").InnerText Me.textBox3.Text = xmlnode("content").InnerText End Sub |
No comments:
Post a Comment