Quantcast
Channel: Code Scratcher » VB Articles
Viewing all articles
Browse latest Browse all 7

How to Read Data From XML File using Windows Application

$
0
0

Our article regarding, how to view; read data from xml file using Windows Application will be very useful for beginners to learn XML Operation’s. View all the xml records in the Datagridview control of the window forms application.

In previous articles we explained How to update data into xml file using Windows Application, how to insert data into xml file using Windows Application. Our previous both the articles give you clear ideas about how to insert and update the information into the XML file. In our both the articles we are using the studentinformations XML for the complete operations. Now we move for the View; read data from xml file using Windows Application.

For this view; read data from xml file, we are using our studentinformaions.xml file to read.

Following are the steps for How to View; read data from xml file using Windows Application.

Add Controls in forms

View all Controls

View all Controls

  • DataGridView controls Id as “DataGridViewStudentRecords
  • Button control Id as view “btnView

Add Code-Behind Source

Add Namespace

Imports System.Xml

Generate Table for Student

BuildStudentTableforXML function Generate StudentTable structure with column name and types.

Try
	Dim dcRollNo = New DataColumn("RollNo", GetType(String))
	Dim dcname = New DataColumn("Name", GetType(String))
	Dim dcCollegeName = New DataColumn("CollegeName", GetType(String))
	Dim dcInterest = New DataColumn("Interest", GetType(String))
	StudentTable.Columns.Add(dcRollNo)
	StudentTable.Columns.Add(dcname)
	StudentTable.Columns.Add(dcCollegeName)
	StudentTable.Columns.Add(dcInterest)
Catch ex As Exception
End Try

Load StudentInformations.XML into the StudentTable

LoadXMLForStudent function will load all the data into StudentTable.

Public Sub LoadXMLForStudent()
	Dim objds As DataSet
	objds = New DataSet
	Dim StrpathOfProductXML As String = Application.StartupPath & "\xml\StudentInformations.xml"
	objds.ReadXml(StrpathOfProductXML)
	If objds.Tables(0).Rows.Count > 0 Then
		StudentTable = objds.Tables(0)
	End If
End Sub

Bind gridview control

Public Sub BindRecordsinGrid()
	DataGridViewStudentRecords.DataSource = StudentTable
	DataGridViewStudentRecords.Columns(0).Width = 190
	DataGridViewStudentRecords.Columns(1).Width = 190
	DataGridViewStudentRecords.Columns(2).Width = 190
	DataGridViewStudentRecords.Columns(3).Width = 190
	DataGridViewStudentRecords.AutoGenerateColumns = False
	DataGridViewStudentRecords.ReadOnly = True
	DataGridViewStudentRecords.AllowUserToAddRows = False
End Sub

View button click

Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
	CheckOperationType(False, False, True)
	BuildStudentTableforXML()
	LoadXMLForStudent()
	BindRecordsinGrid()
End Sub

Save and build your project, now we are moving on final step of view; read data from xml file.

Run Project and Check Output

Output

Output

Source Code

Download project

What do you think of this article? How to Read Data From XML File using Windows Application
If you have ideas about this article, or an opinion on how we can make it better, then let us know by emailing…
help@codescratcher.com

The post How to Read Data From XML File using Windows Application appeared first on Code Scratcher.


Viewing all articles
Browse latest Browse all 7

Trending Articles