Create XML File in VB 2005

Posted on 2010-01-04
Medium Priority
Last Modified: 2013-11-07
Hello All,
I have been tasked to build a XML file with the below format. The only issue is that I have no experience with XML and VB 2005 at all... Please help
<?xml version="1.0" standalone="yes"?>
<DATAPACKET Version="2.0">
      <FIELD attrname="Shipment_BOL" fieldtype="string" WIDTH="10"/>
      <FIELD attrname="Item_Model" fieldtype="string" WIDTH="20"/>
      <FIELD attrname="Cut_Off_Date" fieldtype="date"/>
      <FIELD attrname="Item_Quantity" fieldtype="i4"/>
    <ROW Shipment_BOL="1234567" Item_Model="103ASD" Cut_Off_Date="20091020" Item_Quantity="350"/>
    <ROW Shipment_BOL="1234567" Item_Model="102ASD" Cut_Off_Date="20091020" Item_Quantity="200"/>
    <ROW Shipment_BOL="1234567" Item_Model="101ASD" Cut_Off_Date="20091020" Item_Quantity="175"/>
    <ROW Shipment_BOL="8765432" Item_Model="DDSA101" Cut_Off_Date="20091015" Item_Quantity="400"/>
    <ROW Shipment_BOL="8765432" Item_Model="ASD101" Cut_Off_Date="20091015" Item_Quantity="150"/>

Open in new window

Question by:fullbugg
  • 3
  • 3
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 2000 total points
ID: 26173665
I like the control that you get with the XmlTextWriter, but there is also XML serialization.  


Writing XML in .NET Using XmlTextWriter
Imports System 
Imports System.IO 
Imports System.Xml 

Public Class Sample 
    Public Shared Sub Main() 
        Dim writer As New XmlTextWriter("titles.xml", Nothing) 
        'Write the root element 
        'Write sub-elements 
        writer.WriteElementString("title", "Unreal Tournament 2003") 
        writer.WriteElementString("title", "C&C: Renegade") 
        writer.WriteElementString("title", "Dr. Seuss's ABC") 
        ' end the root element 
        'Write the XML to file and close the writer 
    End Sub 
End Class 

Open in new window


Author Comment

ID: 26173964
Thanks for the reply... the snippet works but even with the URLs help I can get past the <?xml version="1.0" standalone="yes"?>

What methods are there in this control that will generate the rest of the document
<DATAPACKET Version="2.0">

Thanks again for your help...
LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 26174065
The key to using the XmlTextWriter is to learn and understand how to use the write methods (WriteStartDocument, WriteStartElement, etc.):

XmlTextWriter Members

What you are looking for is the WriteStartDocument method.

XmlTextWriter.WriteStartDocument Method (Boolean)
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.


Author Comment

ID: 26174722
Thanks for the reply.

I found another way to do this... it uses a template XML file to generate the row data.

This is in a simple loop to demonstrate the functionality

Const TemplateFile As String = "C:\Documents and Settings\All Users\Desktop\Test XML\template.xml"
        Dim xml_Doc As XmlDocument
        Dim xml_Rowdata As XmlNode
        Dim xml_Row As XmlNode
        Dim xml_Attribut As XmlAttribute
        Dim xmlPfad As String

        xmlPfad = TemplateFile
        xml_Doc = New XmlDocument()

        Dim i As Integer = 0
        For i = 0 To 50
            xml_Rowdata = xml_Doc.SelectSingleNode("DATAPACKET/ROWDATA")
            xml_Row = xml_Doc.CreateNode(XmlNodeType.Element, "ROW", "")
            xml_Attribut = xml_Doc.CreateAttribute("Shipment_BOL")
            xml_Attribut.Value = "ASD123"
            xml_Attribut = xml_Doc.CreateAttribute("Item_Model")
            xml_Attribut.Value = "ModelASD" & i
            xml_Attribut = xml_Doc.CreateAttribute("Cut_Off_Date")
            xml_Attribut.Value = Format(Now, "yyyyMMdd")
            xml_Attribut = xml_Doc.CreateAttribute("Item_Quantity")
            xml_Attribut.Value = 50 + i
        xml_Doc.Save("C:\Documents and Settings\All Users\Desktop\Test XML\" & Format(Now, "yyyyMMdd-HHmmss") & ".xml")
        xml_Doc = Nothing
        xml_Rowdata = Nothing
        xml_Row = Nothing
        xml_Attribut = Nothing
        xmlPfad = Nothing

Open in new window


Author Closing Comment

ID: 31672536
Thanks for all your help and time.
LVL 96

Expert Comment

by:Bob Learned
ID: 26175029
There are definitely plenty of ways to approach that problem.  I only try to provide suggestions.  If you find your own solution, I am always happy with that!!

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question