Total Members: 48

Who's online

There are currently 1 user and 0 guests online.

A xml reader writer in vb.NET

The Extensible Markup Language (XML) is a general-purpose specification for creating custom markup languages. It is classified as an extensible language because it allows its users to define their own elements. Its primary purpose is to facilitate the sharing of structured data across different information systems, particularly via the Internet, and it is used both to encode documents and to serialize data. In the latter context, it is comparable with other text-based serialization languages such as JSON and YAML.

This example will give you a good idea how xml works. First, we need to include the xml namespace at th etop of our file:

Imports System.Xml

Then start with beginning of the module

Module XmlReaderWriter

Then, let define a sub main so to run the demo when you debug your program:



Sub Main()
XMLwriterDemo()
'XMLreaderDemo()
'Console.ReadLine()
End Sub

The sub main calls two subroutine, XMLwriterDemo() and XMLreaderDemo(). Let's start with the first one:
First Create an empty xml file using any editor you prefer.



Private Sub XMLwriterDemo()
Dim strDataFile As String = "C:\Data\newbooks.xml"' Define the path of the xml file in your hard drive
Dim myXmlTextWriter As XmlTextWriter = New XmlTextWriter(strDataFile, Nothing)' Create an instant variable of XmlText Writer
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented' define the formatting of myXmlTextWriter
myXmlTextWriter.WriteStartDocument(False)
' Create parent Node that you want to write your data in
' myXmlTextWriter.WriteDocType("bookstore", Nothing, "books.dtd", Nothing)
myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database")
myXmlTextWriter.WriteStartElement("bookstore")
'Create another nodes under bookstor and start copying data to them
myXmlTextWriter.WriteStartElement("book", Nothing)
myXmlTextWriter.WriteAttributeString("genre", "autobiography")
myXmlTextWriter.WriteAttributeString("publication_date", "1979")
myXmlTextWriter.WriteAttributeString("ISBN", "0-7356-0562-9")
myXmlTextWriter.WriteElementString("title", Nothing, "The Autobiography of Mark Twain")
myXmlTextWriter.WriteStartElement("Author", Nothing)
myXmlTextWriter.WriteElementString("first-name", "Mark")
myXmlTextWriter.WriteElementString("last-name", "Twain")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("price", "7.99")
myXmlTextWriter.WriteEndElement()
'Create another record to under bookstore

myXmlTextWriter.WriteStartElement("book", Nothing)
myXmlTextWriter.WriteAttributeString("genre", "autobiography")
myXmlTextWriter.WriteAttributeString("publication_date", "2005")
myXmlTextWriter.WriteAttributeString("ISBN", "140003003X")
myXmlTextWriter.WriteElementString("title", Nothing, "My Life")
myXmlTextWriter.WriteElementString("Format", Nothing, "Paperback")
myXmlTextWriter.WriteElementString("Pages", Nothing, "1056")
myXmlTextWriter.WriteStartElement("Author", Nothing)
myXmlTextWriter.WriteElementString("first-name", "Bill")
myXmlTextWriter.WriteElementString("last-name", "Clinton")
myXmlTextWriter.WriteEndElement()
myXmlTextWriter.WriteElementString("price", "17.95")
myXmlTextWriter.WriteEndElement()
'end of the xml documents, this will close all open tags

myXmlTextWriter.WriteEndElement()

'Write the XML to file and close the myXmlTextWriter
myXmlTextWriter.Flush()
myXmlTextWriter.Close()
System.Diagnostics.Process.Start(strDataFile)
End Sub


Now, let's let's create a subroutine to read data from the XML file:



Private Sub XMLreaderDemo()
Dim reader As XmlTextReader = New XmlTextReader("C:\Data\newbooks.xml")' Define an instant variable of XmlTextReader
Do While (reader.Read()) ' Loop to go by all elements
Select Case reader.NodeType
Case XmlNodeType.Element 'Display beginning of element.

' Display data in the console
Console.WriteLine("=================================")
Console.WriteLine("Depth = " + reader.Depth.ToString)
Console.WriteLine("Element [" + reader.Name + "]")
If reader.HasAttributes Then 'If attributes exist ' In case of more records, read it.Else exit
While reader.MoveToNextAttribute()
Console.Write(" Attribute [")
Console.Write(reader.Name)
Console.WriteLine("] = {0}", reader.Value)
End While
End If
Case XmlNodeType.Text 'Display the text in each element.
Console.Write(" Value = ")
Console.WriteLine(reader.Value)
End Select
Loop

End Sub

Now you have to close the module:



End Module

Run the program, and then stop it. Go to the Sub main, comment the first line, and uncomment the second and third line. Run it again; what do you see? Easy,ha?