|
-
Jul 27th, 2004, 10:33 PM
#1
Thread Starter
Hyperactive Member
Read Text/Write XML
I want to read a comma de-limited text file and and rewrite it as an XML file. The text file is set up as follows:
FirstName,LastName,AddressLine1, etc.
I thought I could read the text file, insert the values into a struct, and then write the values to an XML file. Here's an example:
Structure Person
Public FName As String
Public LName As String
Public AddressLine1 As String
End Structure
Dim record As Person = New Person()
Dim sr As StreamReader = New StreamReader(textfilename)
While sr.Peek() > -1
record.FName = Mid(sr.ReadLine, 3, 8)
record.LName = Mid(sr.ReadLine, 11, 7)
record.AddressLine1 = Mid(sr.ReadLine, 21, 21)
End While
sr.Close()
I was attempting to write the XML file like this:
Dim xtw As XmlTextWriter = New XmlTextWriter(xmlfilename, Nothing)
With xtw
.WriteStartDocument(False)
.WriteStartElement("Person")
.WriteElementString("FirstName", DP.FName)
.WriteElementString("LastName", DP.LName)
.WriteElementString("AddressLine1", DP.AddressLine1)
.WriteEndElement()
.Flush()
.Close()
End With
I have a couple of problems. First, all that is being written to the xml file is the first name value and the xml tags. Secondly, I need to do this for a large text file. Can I read the entire file into memory and then write it?
Any help would be appreciated.
Last edited by Dman; Jul 28th, 2004 at 08:01 AM.
A cynic knows the price of everything but the value of nothing.
-
Jul 28th, 2004, 08:02 AM
#2
Thread Starter
Hyperactive Member
A cynic knows the price of everything but the value of nothing.
-
Jul 28th, 2004, 08:22 AM
#3
Fanatic Member
You read in your info into <record> variable but output a <DP> variable,
I assume these are two different functions? and that you are passing in
DP as your former record varaible?
Does it work when you process everything at once?
I'm not sure <record> is the best choice for a variable name in the first place.
If the data does not require any user processing, I would write out
the xml file the same time I was reading in the data.
Whether you can read it in memory depends on how "large" is large.
I would lean towards doing it all at the same time, only dealing with
one line of the csv at a time.
-
Jul 28th, 2004, 08:32 AM
#4
Thread Starter
Hyperactive Member
Originally the code where the xml file is written was a function that was passed a structure of type person with the variable name DP. This is all practice code just to get the stuff to work.
A cynic knows the price of everything but the value of nothing.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|