Let’s take a look at an example to create xml file with one level of nesting. The file stores contact information for personal and business purposes. The structure of the xml file expected is as below.
<?xml version="1.0" encoding="utf-8" ?>
<contacts>
<contact type="Personal">
<name></name>
<email>
<primary></primary>
<secondary></pecondary>
</email>
<telephone>
<home></home>
<work></work>
<mobile></mobile>
</telephone>
</contact>
<contact type="Business">
<name>PANAM</name>
<email>
<primary>PANAM@ASPALLIANCE.COM</primary>
<secondary></secondary>
</email>
<telephone>
<home>(732)635-0507</home>
<work>(732)801-8075/work>
<mobile>(732)865-1920</mobile>
</telephone>
</contact>
</contacts>
The following is a code to generate the file in above mentioned format.
Note:
Set TextMode Property of TextBox1 and TextBox2 to “MultiLine”
Set Caption Property of Button1 to “Save XML”
Set Caption Property of Button2 to “Show XML”
Code:
Imports System.Xml
Public Class xmlexample
Inherits System.Web.UI.Page
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Dim filename As String = "xmlnotes.xml"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveXML()
End Sub
Private Sub SaveXML()
Dim xmlwriter As XmlTextWriter
Try
xmlwriter = New XmlTextWriter(Server.MapPath(filename), Nothing) 'default encoding - UTF-8
'indent xml document
xmlwriter.Formatting = Formatting.Indented
'writes the xml declaration with version 1.0
xmlwriter.WriteStartDocument()
'add coment
xmlwriter.WriteComment("This is an xml file generated using XMLTextWriter class")
'write the root node
xmlwriter.WriteStartElement("contacts")
'write parent node
xmlwriter.WriteStartElement("contact")
'define attribute type of contact
xmlwriter.WriteStartAttribute("", "type", "")
'define value for attribute
xmlwriter.WriteString("business")
'close attribute
xmlwriter.WriteEndAttribute()
'write information about children or siblings
xmlwriter.WriteElementString("name", "PANAM")
'add parent node e-mail and detailed information
xmlwriter.WriteStartElement("email")
xmlwriter.WriteElementString("primary", "PANAM@ASPALLIANCE.COM")
xmlwriter.WriteElementString("secondary", "")
xmlwriter.WriteEndElement()
'add parent node telephone and detailed information
xmlwriter.WriteStartElement("telephone")
xmlwriter.WriteElementString("home", "(732)635-0507")
xmlwriter.WriteElementString("work", "(732)801-8075")
xmlwriter.WriteElementString("mobile", "(732)865-1920")
xmlwriter.WriteEndElement()
'close parent node Contact
xmlwriter.WriteEndElement()
'close parent node Contacts
xmlwriter.WriteEndElement()
'close xml declaration tag
xmlwriter.WriteEndDocument()
xmlwriter.Flush()
xmlwriter.Close()
Dim doc As New XmlDocument()
'Preserve white space for readability.
doc.PreserveWhitespace = True
'Load the file.
doc.Load(Server.MapPath(filename))
'Display the XML content to the console.
TextBox1.ForeColor = System.Drawing.Color.Blue
TextBox1.Text = doc.InnerXml
Catch ex As Exception
TextBox1.ForeColor = System.Drawing.Color.Red
TextBox1.Text = ex.Message
End Try
End Sub
End Class