Create XML File in VB 2005

Posted on 2010-01-04
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
    LVL 96

    Assisted Solution

    by:Bob Learned
    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

    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

    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)

    Author Comment

    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

    Thanks for all your help and time.
    LVL 96

    Expert Comment

    by:Bob Learned
    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

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now