Our article regarding, how to insert data into xml file using Windows Application will be very useful for beginners to learn XML Operation’s. For this we are creating xml file and insert the user entry data into XML file.
In previous articles we explained How to Create Windows Forms Application in Visual Studio 2010, How to Play Flash in Windows Forms. Now we move for the insert data into xml file using Windows Application.
We are creating simple xml file with the user input details. Let move on that step-by-step manners. For this we are creating sample windows application in visual studio 2010 using Vb.
Following are the steps for How to insert data into xml file using Windows Application.
For this article we are creating xml file for student details. We are asking to user to input the details of student Like Roll no, Name, College Name, Interest.
Add Controls

Add controls
Add 3 buttons in to the form
- Add with the button Id as “btnAdd”
- Reset with the button Id as “btnReset”
- Submit with the button Id as “btnSubmit”
4 Textbox controls with the label indications
- RollNo with the textbox Id as “txtRollno”
- Name with the textbox Id as “txtName”
- College Name with the textbox Id as “txtCollegeName”
- Interest with the textbox Id as “txtInterset”
One label for success message with the label id “lblMsg”
Add Code-Behind Source
Add namespace
Imports System.Xml
Create XML file and add records to XML file
CreateStudentNodes function create XML file and save to specified location.
Private Sub CreateStudentNodes() Try Dim usageXMLWriter As New XmlWriterSettings() usageXMLWriter.Indent = True usageXMLWriter.NewLineOnAttributes = False Using writer As XmlWriter = XmlWriter.Create(Application.StartupPath & "\xml\StudentInformations.xml", usageXMLWriter) writer.WriteStartDocument() writer.WriteStartElement("StudentDetails") writer.WriteEndElement() writer.WriteEndDocument() writer.Flush() End Using Catch ex As Exception End Try End Sub
AddStudentRecords function open that created XML file and add the student information into that xml file. It will call the function when user click on submit.
Private Sub AddStudentRecords() Try Dim intCountRecords As Integer = 0 If IO.Directory.Exists(Application.StartupPath & "\xml\") = False Then IO.Directory.CreateDirectory(Application.StartupPath & "\xml\") End If Dim FileName As String = Application.StartupPath & "\xml\StudentInformations.xml" If IO.File.Exists(FileName) = False Then CreateStudentNodes() Else Dim objds As DataSet objds = New DataSet objds.ReadXml(Application.StartupPath & "\xml\StudentInformations.xml") intCountRecords = objds.Tables(0).Rows.Count If Not IsNothing(objds) Then objds.Dispose() objds = Nothing End If Dim myDoc As Xml.XmlDocument = New Xml.XmlDocument myDoc.Load(FileName) Dim nodelist1 As XmlNodeList = myDoc.SelectNodes("/StudentDetails") nodelist1.Item(nodelist1.Count) Dim node As XmlNode = myDoc.SelectSingleNode("/StudentDetails") Dim TaskStatusElements1 As XmlElement TaskStatusElements1 = myDoc.CreateElement("Student") With TaskStatusElements1 .SetAttribute("RollNo", txtRollno.Text.ToString()) .SetAttribute("Name", txtName.Text.ToString()) .SetAttribute("CollegeName", txtCollegeName.Text.ToString()) .SetAttribute("Interest", txtInterset.Text.ToString()) '.SetAttribute("StudentId", intCountRecords + 1) End With node.AppendChild(TaskStatusElements1) Dim myWriter As New Xml.XmlTextWriter(FileName, System.Text.Encoding.UTF8) myWriter.Formatting = Xml.Formatting.Indented myDoc.Save(myWriter) myWriter.Close() Catch ex As Exception End Try End Sub
Reset controls
Private Sub ControlReset() txtCollegeName.Text = "" txtInterset.Text = "" txtName.Text = "" txtRollno.Text = "" End Sub
Button clicks
Submit Button click
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click AddStudentRecords() ControlReset() lblMsg.Visible = True End Sub
Reset Button click
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click ControlReset() End Sub
Save and build your project, now we are moving on final step of insert data into xml file.
Run Project and Check Output
Insert Operation

User Input
Success

Node Added Successfully
XML node with User inputs

XML Node With User Inputs
Source Code
help@codescratcher.com
The post How to Insert Data into XML File using Windows Application appeared first on Code Scratcher.