Create XML File in VB.NET

Submitted by: 
Visitors have accessed this post 871 times.

Today in VB.NET, I’m going to teach you how to create a program that also creates an XML file using VB.NET. We all know that XML (Extensible Markup Language) is an easy way to create common information formats and share both the format and the data on the World Wide Web or elsewhere.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application in VB.NET for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application and name your program as XMLSample.

2. Next, add only one Button named Button1 and labeled it as "Create XML". You must design your interface like this:

output

3. Import System.Xml namespace to access all the xml class.

  1. Imports System.Xml

4. Create a method named createNode that has this parameters (string pID, string pName, string pPrice, XmlTextWriter writer). The XmlTextWriter class here is used to write xml file.

  1. Private Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter)
  2. 'write Product as a Start Element
  3. writer.WriteStartElement("Product")
  4. 'write Product_id as a Start Element as a content of Product
  5. writer.WriteStartElement("Product_id")
  6. 'write a string for Product_id
  7. writer.WriteString(pID)
  8. ' end the element of Product_id using /
  9. writer.WriteEndElement()
  10. 'write Product_name as a Start Element as a content of Product
  11. writer.WriteStartElement("Product_name")
  12. 'write a string for Product_name
  13. writer.WriteString(pName)
  14. 'end the element of Product_name using /
  15. writer.WriteEndElement()
  16. 'write Product_price as a Start Element as a content of Product
  17. writer.WriteStartElement("Product_price")
  18. ' write a string for Product_price
  19. writer.WriteString(pPrice)
  20. 'end the element of Product_price using /
  21. writer.WriteEndElement()
  22. 'end the element of Product using /
  23. writer.WriteEndElement()
  24. End Sub

5. For Button1 for writing an XML File, put this code below.

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. 'instatantiate XmlTextWriter that the file name of the xml file will be product.xml
  3. Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8)
  4. 'set WriteStartDocument to true
  5. writer.WriteStartDocument(True)
  6. 'the xml formatting has indention
  7. writer.Formatting = Formatting.Indented
  8. '2 indentions
  9. writer.Indentation = 2
  10. 'Make now the first element into "Table"
  11. writer.WriteStartElement("Table")
  12. 'we will use the createNode method with the parameters of product id, product name, product price, and the writer variable for XmlTextWriter
  13. createNode(1, "Product 1", "1000", writer)
  14. createNode(2, "Product 2", "2000", writer)
  15. createNode(3, "Product 3", "3000", writer)
  16. createNode(4, "Product 4", "4000", writer)
  17. 'end the element with the word table and using /
  18. writer.WriteEndElement()
  19. 'end the file
  20. writer.WriteEndDocument()
  21. 'close the file
  22. writer.Close()
  23. End Sub

Full source code:

  1. Imports System.Xml
  2. Public Class Form1
  3.  
  4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5. Dim writer As New XmlTextWriter("product.xml", System.Text.Encoding.UTF8)
  6. writer.WriteStartDocument(True)
  7. writer.Formatting = Formatting.Indented
  8. writer.Indentation = 2
  9. writer.WriteStartElement("Table")
  10. createNode(1, "Product 1", "1000", writer)
  11. createNode(2, "Product 2", "2000", writer)
  12. createNode(3, "Product 3", "3000", writer)
  13. createNode(4, "Product 4", "4000", writer)
  14. writer.WriteEndElement()
  15. writer.WriteEndDocument()
  16. writer.Close()
  17. End Sub
  18. Private Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter)
  19. writer.WriteStartElement("Product")
  20. writer.WriteStartElement("Product_id")
  21. writer.WriteString(pID)
  22. writer.WriteEndElement()
  23. writer.WriteStartElement("Product_name")
  24. writer.WriteString(pName)
  25. writer.WriteEndElement()
  26. writer.WriteStartElement("Product_price")
  27. writer.WriteString(pPrice)
  28. writer.WriteEndElement()
  29. writer.WriteEndElement()
  30. End Sub
  31.  
  32. End Class

Output:
output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.


Engr. Lyndon Bermoy

IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.