VB.Net program to create an XML file with employee data

Here, we are going to learn how to create an XML file with employee data in VB.Net?
Submitted by Nidhi, on February 08, 2021 [Last updated : March 08, 2023]

Creating an XML file with employee data in VB.Net

Here, we will demonstrate the use of XmlTextWriter class to create an XML file and write employee data inside the file.

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
	<ID>101</ID>
	<FirstName>Amit</FirstName>
	<LastName>Kumat</LastName>
	<Salary>10000</Salary>
</Employees>

Program/Source Code:

The source code to create a file with employee data is given below. The given program is compiled and executed successfully.

VB.Net code to create an XML file with employee data

'VB.NET program to create a file with employee data.

Imports System.Xml
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Dim xmlFile As XmlTextWriter

        xmlFile = New XmlTextWriter("Employee.xml", Encoding.UTF8)

        xmlFile.WriteStartDocument()
        xmlFile.WriteStartElement("Employees") ' Root.
        xmlFile.WriteElementString("ID", "101")
        xmlFile.WriteElementString("FirstName", "Amit")
        xmlFile.WriteElementString("LastName", "Kumat")
        xmlFile.WriteElementString("Salary", "10000")
        xmlFile.WriteEndElement()
        xmlFile.WriteEndDocument()

        Console.WriteLine("Employee.xml file created successfully")

        xmlFile.Flush()
        xmlFile.Close()
    End Sub
End Module

Output

Employee.xml file created successfully
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(), and here we imported the System.Xml namespace to use XmlTextWriter class.

The Main() function is the entry point for the program. In the Main() function, we created the object of XmlTextWriter class to create the "Employee.xml" file and then write data into an XML file using methods of XmlTextWriter class.

VB.Net File Handling Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.