Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert delimited file to xml document

How-to

Convert delimited file to xml document

by  PsychoCoder  Posted    (Edited  )
In the application I'm currently working on I had the need to parse a delimited text file and create an XML document out of it. It took some time with the XML and StreamReader Namespaces but I finally wrote what I needed (took so much time simply because I haven't done much work with XML to date)

I created a seperate class file for the converting, in the class file you need the following Imports:
Code:
Imports System
Imports System.IO
Imports System.Collections
Imports System.Data
Imports System.Text
The procedure that actually parses the delimited file you pass 5 required items and 1 optional one.

Required
sDelimited: Delimiter used to seperate items in the list
sFilePath: Path and file name of the delimited file
sDataSetName: Name of the dataset you want(Ends up being the first StartTag in the XML document.
sNameSpace: The NameSpace for the XML Document
sDataTableName: DataTable name to add to the DataSet (Ends up being your Items StartTag)

Optional
sPrefix: Prefix for the XML Document

Now for the procedure to do all this:
Code:
Public Sub ReadList(ByVal sDelimiter As String, ByVal sFilePath As String, ByVal sDataSetName As String, _
    ByVal sNameSpace As String, ByVal sDataTableName As String, Optional ByVal sPrefix As String = "")
   'Check to see if the demimited file exists
   If Not System.IO.File.Exists(sFilePath) Then    'Doesnt exist
       'Write to error log and let user know
       oEvents.WriteToLog("Host Error list could not be found.", "", "ReadErrorList Error")
       MsgBox("The Host Error List could not be found." & vbCrLf & _
            "Please contact your software support", MsgBoxStyle.Critical)
       Exit Sub
   Else    'Delimited file exists
       'Now check to see if the XML file already exists
       If Not System.IO.File.Exists(Application.StartupPath & "\HostErrorList.xml") Then   'Doesnt exist
           'Create the XML document from the delimited file
           Try
               dsErrorList.DataSetName = sDataSetName  'Set the DataSet name
               dsErrorList.Namespace = sNameSpace  'Set the XML Document NameSpace
               dsErrorList.Prefix = sPrefix    'Set the prefix (Optional)
               dsErrorList.Tables.Add(sDataTableName)  'Add the table
               'Read the delimited file
               Dim srErrorList As New StreamReader(sFilePath)
               'Go to the top of the file
               srErrorList.BaseStream.Seek(0, SeekOrigin.Begin)
               'Add in the Header Columns
               For Each sFields In srErrorList.ReadLine().Split(sDelimiter)    'Loop through header list
     dsErrorList.Tables(0).Columns.Add(sFields)
               Next
               'Now add in the Rows
               dtErrorList = dsErrorList.Tables(0)
               While (srErrorList.Peek() > -1) 'Loop while there are rows in the delimited file
                   drErrorList = dtErrorList.NewRow()
                   'Add the items to the DataSet
                   For Each sFields In srErrorList.ReadLine().Split(sDelimiter)
                       drErrorList(iCounter) = sFields
                       iCounter += 1
                   Next
                   iCounter = 0
                   'Add the new row to the DataSet
                   dtErrorList.Rows.Add(drErrorList)
               End While
               'Write the DataSet to an XML document
                    dsErrorList.WriteXml(Application.StartupPath & "\HostErrorList.xml")
           Catch ex As Exception   'Delimited file doesnt exist
               'Write to error file and let user know
               oEvents.WriteToLog(ex.Message, ex.StackTrace, "ReadErrorList Error(Reading file)")
               MsgBox("The host error list could not be read." & vbCrLf & _
                    "Please contact your software support", MsgBoxStyle.Critical)
               Exit Sub
           End Try
       End If
   End If
End Sub
The oError variable is one created from a class file I have that writes all errors to an error log, it also writes to the Event Viewer whenever the application opens and closes.

To call the procedure in the Form1_Load you want the XML document created just add the line:
Code:
oError.ReadList("!", Application.StartupPath & "\ErrorList.txt", "HostErrorList", "ErrorList", "ErrorDetails", "V1.0") [i]The names in the call are specific to my application, you will have your own.[/i]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top