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..

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.

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

thanks..i already got it..

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..

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

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
<phrase subcategory="Chapter 7">Cardiac cycle </phrase>
<phrase subcategory="Chapter 7">Cardiogenic shock</phrase>
<phrase subcategory="Chapter 7">Carotid audiofrequency analysis </phrase>
<phrase subcategory="Chapter 7">Cerebral vascular accident</phrase>
<phrase subcategory="Chapter 7">Compression sclerotherapy</phrase>
<phrase subcategory="Chapter 7">Computed tomography</phrase>


How can i do this?

Thanks for the help.


Kevin

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

sorry not the array, the for..next

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

nice one! It works perfectly! :)

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.

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

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>

What if my file estructure is like:

Charles,Johnson,2/11/40,Jake, Blake,3/22/53,June,Anderson,11/29/66

Any helps please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.