banner



How To Create Xml File In Vb6

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. End Sub

Full source code:

            
  1. Imports System. Xml

  2. Public Class Form1

  3. Private Sub Button1_Click( ByVal sender As System. Object, ByVal e As System. EventArgs ) Handles Button1. Click

  4. Dim writer As New XmlTextWriter( "product.xml", System. Text . Encoding . UTF8 )

  5. writer. WriteStartDocument ( True )

  6. writer. Formatting = Formatting. Indented

  7. writer. Indentation = 2

  8. writer. WriteStartElement ( "Table" )

  9. createNode( 1, "Product 1", "1000", writer)

  10. createNode( 2, "Product 2", "2000", writer)

  11. createNode( 3, "Product 3", "3000", writer)

  12. createNode( 4, "Product 4", "4000", writer)

  13. writer. WriteEndElement ( )

  14. writer. WriteEndDocument ( )

  15. End Sub

  16. Private Sub createNode( ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter)

  17. writer. WriteStartElement ( "Product" )

  18. writer. WriteStartElement ( "Product_id" )

  19. writer. WriteString (pID)

  20. writer. WriteEndElement ( )

  21. writer. WriteStartElement ( "Product_name" )

  22. writer. WriteString (pName)

  23. writer. WriteEndElement ( )

  24. writer. WriteStartElement ( "Product_price" )

  25. writer. WriteString (pPrice)

  26. writer. WriteEndElement ( )

  27. writer. WriteEndElement ( )

  28. End Sub

  29. 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

How To Create Xml File In Vb6

Source: https://www.sourcecodester.com/tutorials/visual-basic-net/7604/create-xml-file-vbnet.html

Posted by: tranwastookey.blogspot.com

0 Response to "How To Create Xml File In Vb6"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel