Not knowing your file structure, this is what I came up with.
File structure:
Charles,Johnson,2/11/40
Jake, Blake,3/22/53
June,Anderson,11/29/66 Code:
Imports System.Xml
Imports System.Text
Public Class Form1
Dim path As String = "d:\hold\"
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
' Read the text document and put in in an array
Dim myText As String = My.Computer.FileSystem.ReadAllText(path & "test.txt")
Dim ary As String() = myText.Split(vbCrLf)
' Define the xml text writer
Dim writer As New XmlTextWriter(path & "test.xml", Encoding.ASCII)
' Use indenting
writer.Formatting = Formatting.Indented
writer.Indentation = 4
' Start the document. This is the header stating this is xml 1.0.
writer.WriteStartDocument(True)
' Write the main element
writer.WriteStartElement("Contacts")
' Now write the contacts data for each element in the array
For x As Integer = 0 To ary.Length - 1
WriteContact(writer, ary(x))
Next
' Close the Contacts element
writer.WriteEndElement()
' Close the document
writer.WriteEndDocument()
' Close the writer
writer.Close()
End Sub
Private Sub WriteContact(ByVal wr As XmlWriter, ByVal str As String)
' Break string into attributes
Dim ary As String() = str.Split(",")
' Write element name
wr.WriteStartElement("Contact")
' Write first name
wr.WriteStartElement("FirstName")
wr.WriteString(ary(0))
wr.WriteEndElement()
' Write last name
wr.WriteStartElement("LastName")
wr.WriteString(ary(1))
wr.WriteEndElement()
' Write birth date
wr.WriteStartElement("BirthDate")
wr.WriteString(ary(2))
wr.WriteEndElement()
' Finaly close the Contact element
wr.WriteEndElement()
End Sub Result:
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<Contacts>
<Contact>
<FirstName>Charles</FirstName>
<LastName>Johnson</LastName>
<BirthDate>2/11/40</BirthDate>
</Contact>
<Contact>
<FirstName>
Jake</FirstName>
<LastName> Blake</LastName>
<BirthDate>3/22/53</BirthDate>
</Contact>
<Contact>
<FirstName>
June</FirstName>
<LastName>Anderson</LastName>
<BirthDate>11/29/66</BirthDate>
</Contact>
</Contacts> Hope this helps.
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58