A sample of operating XML data in ASP.NET 2.0(VB.NET)
Microsoft .NET introduces a new suite of XML APIs built on industry standards such as DOM, XPath, XSD, and XSLT. The .NET Framework XML classes also offer convenience, better performance, and a more familiar programming model, tightly coupled with the new .NET data access APIs—ADO .NET. XmlWriter, XmlReader, and XmlNavigator classes and classes that derive from them, including XMLTextReader and XMLTextWriter, encapsulate a number of functionalities that previously had to be accomplished manually. This tutorial will show you a sample of how to operate XML in ASP.NET and VB.NET.
The
System.Xml namespace contains the
XmlDocument Class .we can use this class to operate xml file.
At first, import the namespace of System.Xml and System.Data
Imports System.Xml Imports System.Data |
Add four buttons to web page, Add, modify, Delete and Clear
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 |
The front page of Default.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Default</title> </head> <body> <form id="form1" runat="server"> <div> <fieldset style='height: 401px'> <legend>Operate Xml</legend>author: <asp:TextBox ID="TextBox1" runat="server" Width="231px"></asp:TextBox><br /> title: <asp:TextBox ID="TextBox2" runat="server" Width="231px"></asp:TextBox><br /> content:<asp:TextBox ID="TextBox3" runat="server" Width="231px"></asp:TextBox> <asp:Label ID="lblSelectIndex" runat="server" Visible="False"></asp:Label><br /> <asp:Button ID="Button1" runat="server" Text="Add" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="modify" /> <asp:Button ID="Button3" runat="server" Text="Delete" /> <asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="Clear" /><br /> <br /> <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="428px" AutoGenerateColumns="False"> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <Columns> <asp:CommandField CancelText="Cancel" DeleteText="Delete" EditText="Modify" InsertText="Insert" NewText="New" SelectText="Select" ShowSelectButton="True" UpdateText="Update" /> <asp:BoundField DataField="author" HeaderText="Author" /> <asp:BoundField DataField="title" HeaderText="Tilte" /> <asp:BoundField DataField="content" HeaderText="Content" /> </Columns> <RowStyle BackColor="#E3EAEB" /> <EditRowStyle BackColor="#7C6F57" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </fieldset>
</div> </form> </body> </html> |
The code behind the front page as followsImports System.Xml Imports System.Data
Partial Class _Default Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load If Me.IsPostBack = False Then loadXmlData() End If End Sub 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
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged Me.lblSelectIndex.Text = Me.GridView1.SelectedIndex.ToString() If Me.lblSelectIndex.Text.Equals("") Then FindXmlData(Int32.Parse(Me.lblSelectIndex.Text)) End If End Sub 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
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
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
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
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 End Class |
Filed under: SQL Transaction, ASP.NET, SQL commit, Transact-SQL, SQL, Transaction, VB.NET, C#.NET, Image, save image, image database