Read and Write XML files using VB.NET.


In this article, you will know how to read and Write XML files using VB.NET. Procedure for XML files using VB.NET. read and write.

Read and write XML files using VB.NET.


This is a VB.NET sample code to read and Write XML files


' ------------------------------------------------------------
' Copyright ©2001 Mike G --> IvbNET.COM
' All Rights Reserved, http://www.ivbnet.com
' EMAIL : webmaster@ivbnet.com
' ------------------------------------------------------------
' You are free to use this code within your own applications,
' but you are forbidden from selling or distributing this
' source code without prior written consent.
' ------------------------------------------------------------


Private Sub cmdWite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWite.Click
Call Write_XML()

End Sub

Private Sub Write_XML()
Dim XMLobj As Xml.XmlTextWriter
Dim enc As New System.[Text].UnicodeEncoding()
XMLobj = New Xml.XmlTextWriter("C:\test.xml", enc)

XMLobj.Formatting = Xml.Formatting.Indented
XMLobj.Indentation = 3
XMLobj.WriteStartDocument()

XMLobj.WriteStartElement("Books")
XMLobj.WriteStartElement("Book")

XMLobj.WriteAttributeString("ISBN", "100000000")
XMLobj.WriteAttributeString("Title", "IvbNET.com")
XMLobj.WriteAttributeString("Price", "50.00")

XMLobj.WriteEndElement()
XMLobj.WriteEndElement()

XMLobj.Close()
MsgBox("Done", MsgBoxStyle.Exclamation, "XML")
End Sub

Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRead.Click
Call Read_XML()
End Sub

Private Sub Read_XML()
Dim XMLReader As Xml.XmlReader
XMLReader = New Xml.XmlTextReader("C:\test.xml")

While XMLReader.Read
Select Case XMLReader.NodeType
Case Xml.XmlNodeType.Element
Debug.WriteLine(XMLReader.Name)
If XMLReader.AttributeCount > 0 Then
While XMLReader.MoveToNextAttribute
Debug.WriteLine(XMLReader.Name & "->" & XMLReader.Value)
End While
End If
Case Xml.XmlNodeType.Text
Debug.WriteLine(XMLReader.Value)
Case Xml.XmlNodeType.Comment
Debug.WriteLine(XMLReader.Value)
End Select
End While
XMLReader.Close()
MsgBox("Done", MsgBoxStyle.Exclamation, "XML")
End Sub
End Class





Comments

Guest Author: rose01 Mar 2012

Good Article.. it helps me a lot. thanks for the code



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: