954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

convert .txt file to xml..pls help

Can anyolne help me in coverting .txt file to xml?..i tried codes from differrent sites but didn't help..please help,.........thanks in advance..

lerkei
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

Hi..tnx for the reply, how can I view the output of my codes?..tnx..

lerkei
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

thanks..i already got it..

lerkei
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

another thing sir, what if my text file has a certain code that should be placed in a specific tag in the xml?..


sample:

NL%BROWN/JOHN MR;


NL% is an indicator that in every text that has this NL%, must be placed inside a certain tag in the xml file.

Need your help,Thanks..

lerkei
Light Poster
36 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Sorry been on vacation.
Give an example what the xml will look like.

waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

Hi the code help out but I have another question? My data is just a single line of data in a txt form (ie)
Cardiac cycle
Cardiogenic shock
Carotid audiofrequency analysis
Cerebral vascular accident
Compression sclerotherapy
Computed tomography

and I would like the output to be
Cardiac cycle Cardiogenic shockCarotid audiofrequency analysis Cerebral vascular accidentCompression sclerotherapyComputed tomography


How can i do this?

Thanks for the help.


Kevin

vanwinkk
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Hi, please i need help as soon as possible (very urgent)
I'm writing a code in vb.net to convert text file to xml, i tried this code but there is an error in the array thing..so no output is viewed

wr.WriteStartElement("LastName")
wr.WriteString(ary(1))
wr.WriteEndElement()
wr.WriteStartElement("BirthDate")
wr.WriteString(ary(2))
wr.WriteEndElement()

the error occured here.. i don't know what to do..
please repy as soon as possible

Spooty
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

sorry not the array, the for..next

Spooty
Newbie Poster
2 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Hi waynespangler

Could you please let me know, how I can convert the text file to XML when they are separated with tab? Like this:

1363809 ANTHONY ABRU 178 North AVE Lexington MA 01540 US 3 393368 906.1 V587.7 443.9 427.31

1363809 STEVEN Hana 487 Homa STREET Wale MA 01550 US 1 C0F0S0 392978 707.15 250.00 401.9 585.9

ShawnSun
Newbie Poster
1 post since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

nice one! It works perfectly! :)

myanime
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

THANKS! :)

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.

myanime
Newbie Poster
2 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

You would modify str.Split(",") to str.Split(" ").

darkly77
Newbie Poster
2 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry that last reply was to ShawnSun.

Does anyone know how to avoid writing on a new line when converting? For instance, I don't want:

<FirstName>
Jake</FirstName>


I just want

<FirstName>Jake</FirstName>
darkly77
Newbie Poster
2 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You