XmlWriter. This class generates an XML document using code. There are several ways to do this. But the XmlWriter type is one of the oldest and most reliable ways.
Some notes. We present a reliable way to generate XML files in the VB.NET language. Though XmlWriter is not the simplest way to generate XML, it is reliable and fast.
Example. We introduce the Employee class, and Main, which writes the XML file. The Employee class holds the ID, first name, last name, and salary of each employee.
Imports System.Xml
Module XmlModule
''' <summary>
''' Employee type.
''' </summary>
Class Employee
Public Sub New(ByVal id As Integer, ByVal firstName As String, _
ByVal lastName As String, ByVal salary As Integer)
' Set fields.
Me._id = id
Me._firstName = firstName
Me._lastName = lastName
Me._salary = salary
End Sub
' Storage of employee data.
Public _firstName As String
Public _id As Integer
Public _lastName As String
Public _salary As Integer
End Class
Sub Main()
' Create array of employees.
Dim employees(2) As Employee
employees(0) = New Employee(1, "Prakash", "Rangan", 70000)
employees(1) = New Employee(5, "Norah", "Miller", 21000)
employees(2) = New Employee(17, "Cecil", "Walker", 60000)
' Create XmlWriterSettings.
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
' Create XmlWriter.
Using writer As XmlWriter = XmlWriter.Create("C:\employees.xml", settings)
' Begin writing.
writer.WriteStartDocument()
writer.WriteStartElement("Employees") ' Root.' Loop over employees in array.
Dim employee As Employee
For Each employee In employees
writer.WriteStartElement("Employee")
writer.WriteElementString("ID", employee._id.ToString)
writer.WriteElementString("FirstName", employee._firstName)
writer.WriteElementString("LastName", employee._lastName)
writer.WriteElementString("Salary", employee._salary.ToString)
writer.WriteEndElement()
Next
' End document.
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
End Sub
End Module<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<ID>1</ID>
<FirstName>Prakash</FirstName>
<LastName>Rangan</LastName>
<Salary>70000</Salary>
</Employee>
<Employee>
<ID>5</ID>
<FirstName>Norah</FirstName>
<LastName>Miller</LastName>
<Salary>21000</Salary>
</Employee>
<Employee>
<ID>17</ID>
<FirstName>Cecil</FirstName>
<LastName>Walker</LastName>
<Salary>60000</Salary>
</Employee>
</Employees>
Discussion. You could change the Employee class to use properties instead of public fields. This can sometimes create more useful abstractions for object-oriented designs.
Also The data inside the employee array could be acquired from another data source, such as a Windows Forms control or a database server.
Summary. The XmlWriter type makes generating XML files much easier. You could just manually print out the XML file. But this usually results in confusing code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 6, 2024 (simplify).