XML file into a Text file - Array problem

Torsan

New member
Joined
Jun 5, 2004
Messages
1

This program is supposed to import data from a text file (2 items on each row in the text file) into an array, and export the data into a XML file.

I am not able to create the array correctly, and make the program loop through the array to create the xml file.

Please help of how to enhance this script including the code. I would need somebody to show me how the array, and the loop should be created.

Thanks for any help.


Module Module1

Sub Main()

Console.WriteLine("Converting text file to XML.")

CreateFile()

End Sub

Private Sub CreateFile()

Dim strFields(2) As String

Dim strLine As String

Dim objReader As IO.TextReader

objReader = IO.File.OpenText("Inventory2.txt")

'Import Text File

Do While objReader.Peek > -1

strLine = objReader.ReadLine

strFields = strLine.Split(ControlChars.Tab)

Loop

'Export into a XML file

Dim objXMLFile As XmlTextWriter

objXMLFile =
New XmlTextWriter("xmlInventory.xml", Nothing)

objXMLFile.Formatting = Formatting.Indented

objXMLFile.Indentation = 4

objXMLFile.IndentChar = Convert.ToChar(" ")

objXMLFile.WriteStartDocument()

objXMLFile.WriteStartElement("Inventory")

Dim i As Integer

i = 0

For i = 0 To strFields.Length



objXMLFile.WriteStartElement("Location")

objXMLFile.WriteAttributeString("format", "string")

objXMLFile.WriteString(strFields(0))

objXMLFile.WriteEndElement()

objXMLFile.WriteStartElement("Product")

objXMLFile.WriteAttributeString("format", "string")

objXMLFile.WriteString(strFields(1))

objXMLFile.WriteEndElement()

i += 1

Next i

objXMLFile.Close()

End Sub



End
Module

 
first and formost, i see the problem with your array:

you've got it set up to only hold two values at any given time : strFields(0) and strFields(1).

If you want to keep it set up to only hold two values at any given time, you're going to need to have it write the data to the xml file immediatly after you read it from the text file.

If you want to keep the current logic of read the whole text file into the array and then write the data from the array to the xml file, I would suggest a big-ass two dimensional array: strFields(x,y) where x is the product and y is the actual amount of product (or something to that effect).

here's the vb6 (understandable) explaination of arrays: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconarrays.asp

and here's the nitty-gritty .net class definition of arrays: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemArrayClassTopic.asp

the rest of it looks good.
 
Back
Top