INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- Turn Off Ad Banners
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...I have tons of books, have book marked tons of tutorials, which have helped, but this forum has answered those "impossible to find" solutions. I am thrilled with this site..."
Geography
Where in the world do Tek-Tips members come from?
|
Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ
|
How-to
|
Convert delimited file to xml document
Posted: 5 Feb 07
|
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:
CODEImports 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:
CODEoError.ReadList("!", Application.StartupPath & "\ErrorList.txt", "HostErrorList", "ErrorList", "ErrorDetails", "V1.0") The names in the call are specific to my application, you will have your own. |
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 FAQ Index
Back to Visual Basic(Microsoft) -VB.NET 2002-2008 Forum
My FAQ Archive
Email This FAQ To A Friend |
|
 |
|