Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The Extensible Markup Language (XML) is a markup language much like HTML or SGML.
This is recommended by the World Wide Web Consortium and available as an open
standard.
The System.Xml namespace in the .Net Framework contains classes for processing XML
documents. Following are some of the commonly used classes in the System.Xml
namespace.
Simple API for XML (SAX) : Here, you register callbacks for events of interest
and then let the parser proceed through the document. This is useful when your
documents are large or you have memory limitations, it parses the file as it reads
it from disk, and the entire file is never stored in memory.
Document Object Model (DOM) API : This is World Wide Web Consortium
recommendation wherein the entire file is read into memory and stored in a
hierarchical (tree-based) form to represent all the features of an XML document.
SAX obviously can't process information as fast as DOM can when working with large files.
On the other hand, using DOM exclusively can really kill your resources, especially if used
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 3/12
3/8/2018 VB.Net - XML Processing
SAX is read-only, while DOM allows changes to the XML file. Since these two different APIs
literally complement each other there is no reason why you can't use them both for large
projects.
For all our XML code examples, let's use a simple XML file movies.xml as an input:
<?xml version="1.0"?>
The XmlReader class is used to read XML data in a fast, forward-only and non-cached
manner. It reads an XML document or a stream.
Example 1
This example demonstrates reading XML data from the file movies.xml.
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 4/12
3/8/2018 VB.Net - XML Processing
Add a label in the form and change its text to 'Movies Galore'.
Add three list boxes and three buttons to show the title, type and description of a
movie from the xml file.
Imports System.Xml
Public Class Form1
Execute and run the above code using Start button available at the Microsoft Visual Studio
tool bar. Clicking on the buttons would display, title, type and description of the movies
from the file.
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 5/12
3/8/2018 VB.Net - XML Processing
The XmlWriter class is used to write XML data into a stream, a file or a TextWriter object.
It also works in a forward-only, non-cached manner.
Example 2
Let us create an XML file by adding some data at runtime. Take the following steps:
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim xws As XmlWriterSettings = New XmlWriterSettings()
xws.Indent = True
xws.NewLineOnAttributes = True
Dim xw As XmlWriter = XmlWriter.Create("authors.xml", xws)
xw.WriteStartDocument()
xw.WriteStartElement("Authors")
xw.WriteStartElement("author")
xw.WriteAttributeString("code", "1")
xw.WriteElementString("fname", "Zara")
xw.WriteElementString("lname", "Ali")
xw.WriteEndElement()
xw.WriteStartElement("author")
xw.WriteAttributeString("code", "2")
xw.WriteElementString("fname", "Priya")
xw.WriteElementString("lname", "Sharma")
xw.WriteEndElement()
xw.WriteStartElement("author")
xw.WriteAttributeString("code", "3")
xw.WriteElementString("fname", "Anshuman")
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 6/12
3/8/2018 VB.Net - XML Processing
xw.WriteElementString("lname", "Mohan")
xw.WriteEndElement()
xw.WriteStartElement("author")
xw.WriteAttributeString("code", "4")
xw.WriteElementString("fname", "Bibhuti")
xw.WriteElementString("lname", "Banerjee")
xw.WriteEndElement()
xw.WriteStartElement("author")
xw.WriteAttributeString("code", "5")
xw.WriteElementString("fname", "Riyan")
xw.WriteElementString("lname", "Sengupta")
xw.WriteEndElement()
xw.WriteEndElement()
xw.WriteEndDocument()
xw.Flush()
xw.Close()
WebBrowser1.Url = New Uri(AppDomain.CurrentDomain.BaseDirectory + "authors.xml")
End Sub
End Class
Execute and run the above code using Start button available at the Microsoft Visual Studio
tool bar. Clicking on the Show Author File would display the newly created authors.xml file
on the web browser.
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 7/12
3/8/2018 VB.Net - XML Processing
Following are some of the commonly used methods of the XmlDocument class:
1 AppendChild
Adds the specified node to the end of the list of child nodes, of this node.
2 CreateAttribute(String)
3 CreateComment
4 CreateDefaultAttribute
Creates a default attribute with the specified prefix, local name and namespace
URI.
5 CreateElement(String)
Creates an XmlNode with the specified node type, Name, and NamespaceURI.
9 CreateProcessingInstruction
10 CreateSignificantWhitespace
11
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 8/12
3/8/2018 VB.Net - XML Processing
CreateTextNode
12 CreateWhitespace
13 CreateXmlDeclaration
14 GetElementById
15 GetElementsByTagName(String)
16 GetElementsByTagName(String, String)
17 InsertAfter
Inserts the specified node immediately after the specified reference node.
18 InsertBefore
Inserts the specified node immediately before the specified reference node.
19 Load(Stream)
20 Load(String)
21 Load(TextReader)
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 9/12
3/8/2018 VB.Net - XML Processing
22 Load(XmlReader)
23 LoadXml
24 PrependChild
Adds the specified node to the beginning of the list of child nodes for this node.
25 ReadNode
Creates an XmlNode object based on the information in the XmlReader. The reader
must be positioned on a node or attribute.
26 RemoveAll
Removes all the child nodes and/or attributes of the current node.
27 RemoveChild
28 ReplaceChild
29 Save(Stream)
30 Save(String)
31 Save(TextWriter)
32 Save(XmlWriter)
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 10/12
3/8/2018 VB.Net - XML Processing
Example 3
In this example, let us insert some new nodes in the xml document authors.xml and then
show all the authors' first names in a list box.
Add the authors.xml file in the bin/Debug folder of your application( it should be
there if you have tried the last example)
Add a list box and a button control in the form and set the text property of the
button control to Show Authors.
Imports System.Xml
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set the caption bar text of the form.
Me.Text = "tutorialspoint.com"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim xd As XmlDocument = New XmlDocument()
xd.Load("authors.xml")
Dim newAuthor As XmlElement = xd.CreateElement("author")
newAuthor.SetAttribute("code", "6")
Dim fn As XmlElement = xd.CreateElement("fname")
fn.InnerText = "Bikram"
newAuthor.AppendChild(fn)
Dim ln As XmlElement = xd.CreateElement("lname")
ln.InnerText = "Seth"
newAuthor.AppendChild(ln)
xd.DocumentElement.AppendChild(newAuthor)
Dim tr As XmlTextWriter = New XmlTextWriter("movies.xml", Nothing)
tr.Formatting = Formatting.Indented
xd.WriteContentTo(tr)
tr.Close()
Dim nl As XmlNodeList = xd.GetElementsByTagName("fname")
For Each node As XmlNode In nl
ListBox1.Items.Add(node.InnerText)
Next node
End Sub
End Class
Execute and run the above code using Start button available at the Microsoft Visual Studio
tool bar. Clicking on the Show Author button would display the first names of all the
authors including the one we have added at runtime.
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 11/12
3/8/2018 VB.Net - XML Processing
Tutorials Point (India) Pvt. Ltd.
YouTube 56K
https://www.tutorialspoint.com/vb.net/vb.net_xml_processing.htm 12/12