Here in this, on the load method, we are reading the xml file which is created using a DataSet. If it is not getting created it is been catched and it is created. When the Fill Button in the form is clicked after entering the values - Name and Place, a new row is been created and a xml file is generated using the DataSet.WriteXml method. The DataTable, DataColumn, DataRow are used for this purposes.
Then for binding the DataGrid, we use the DataGird.DataSource =the dataset.
Method2:
The second method is to use the XmlDocument. This class uses the xmlTextWriter and using that the xmlfile is obtained.
Example:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Xml
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Fill As System.Web.UI.WebControls.Button
Protected WithEvents txtPlace As System.Web.UI.WebControls.TextBox
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Put user code to initialize the page here
End If
End Sub
Private Sub Fill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fill.Click
Try
Dim filename As String = Server.MapPath("sample2.xml")
Dim xmlDoc As New XmlDocument
Try
xmlDoc.Load(filename)
Catch
End Try
Dim root As XmlNode = xmlDoc.DocumentElement
Dim childNode As XmlElement = xmlDoc.CreateElement("Employee")
root.AppendChild(childNode)
Dim childNode2 As XmlElement = xmlDoc.CreateElement("Name")
Dim textNode1 As XmlText = xmlDoc.CreateTextNode(txtName.Text)
childNode2.AppendChild(textNode1)
childNode.AppendChild(childNode2)
Dim childNode3 As XmlElement = xmlDoc.CreateElement("Place")
Dim textNode2 As XmlText = xmlDoc.CreateTextNode(txtPlace.Text)
childNode3.AppendChild(textNode2)
childNode.AppendChild(childNode3)
xmlDoc.Save(filename)
Catch ex As Exception
Response.Write(ex.ToString())
End Try
End Sub
End Class
The main point to be noted here is that we are not using any dataset here. Both the two methods use and produce the same xml file as output. But the difference is that, we cannot bind it to a DataGrid as we are not using any DataSets to be indulged in this. There are many other methods also ie., by using XMLDataDocument also.
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).