Hi,

I'm not exactly sure if this is the correct question to be asked but I will tell you what I am attempting. I have an ASCII txt file with data in it. It does not have headers. I am looking for a way to convert this data into an XML file.

Sample txt file there is no "," or space between datas

5076738D120.03.0351861469-0120.02.2012

5076738D120.03.0352861469-0221.02.2012

and xml file

<?xml version="1.0" encoding="ISO-8859-9"?>

<BANK_VOUCHERS>
<BANK_VOUCHER DBOP="INS" >
<NUMBER>00005671</NUMBER> - constant
<DIVISION>10</DIVISION> - constant
<DEPARMENT>1</DEPARMENT> - constant
<TYPE>3</TYPE> - constant
<NOTES1>23.02.12 GLN HVL </NOTES1> - constant
<CREATED_BY>21</CREATED_BY>- constant
<TRANSACTIONS>

<TRANSACTION>
<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant
<ARP_CODE>120.03.0351</ARP_CODE>
<DATE>20.02.2012</DATE>
<MODULENR>861469-01</MODULENR>
<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant
<AFFECT_RISK>0</AFFECT_RISK> - constant
</TRANSACTION>

<TRANSACTION>
<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant
<ARP_CODE>120.03.0352</ARP_CODE>
<DATE>21.02.2012</DATE>
<MODULENR>861469-02</MODULENR>
<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant
<AFFECT_RISK>0</AFFECT_RISK> - constant
</TRANSACTION>

</TRANSACTIONS>
</BANK_VOUCHER>
</BANK_VOUCHERS>

Please show us some portion of your code so we can help you. By the way what is your problem actually??

Please show us some portion of your code so we can help you. By the way what is your problem actually??

i got txt file like this;
5076738D120.03.0351861469-0120.02.2012
5076738D120.03.0352861469-0221.02.2012

and want to convert it to xml file like;

<?xml version="1.0" encoding="ISO-8859-9"?>

<BANK_VOUCHERS>
<BANK_VOUCHER DBOP="INS" >
<NUMBER>00005671</NUMBER> - constant
<DIVISION>10</DIVISION> - constant
<DEPARMENT>1</DEPARMENT> - constant
<TYPE>3</TYPE> - constant
<NOTES1>23.02.12 GLN HVL </NOTES1> - constant
<CREATED_BY>21</CREATED_BY>- constant
<TRANSACTIONS>

<TRANSACTION>
<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant
<ARP_CODE>120.03.0351</ARP_CODE>
<DATE>20.02.2012</DATE>
<MODULENR>861469-01</MODULENR>
<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant
<AFFECT_RISK>0</AFFECT_RISK> - constant
</TRANSACTION>

<TRANSACTION>
<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant
<ARP_CODE>120.03.0352</ARP_CODE>
<DATE>21.02.2012</DATE>
<MODULENR>861469-02</MODULENR>
<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant
<AFFECT_RISK>0</AFFECT_RISK> - constant
</TRANSACTION>

</TRANSACTIONS>
</BANK_VOUCHER>
</BANK_VOUCHERS>

Ok a form with one text box two button and one listbox

Public Class Form1
    Dim xmlformatpart1, xmlformatpart2, TransFormat As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        xmlformatpart1 = "'"
        xmlformatpart2 = ""
        TransFormat = ""

        xmlformatpart1 = "<?xml version='1.0' encoding='ISO-8859-9'?>"
        xmlformatpart1 += vbNewLine + "<BANK_VOUCHERS>"
        xmlformatpart1 += vbNewLine + "<BANK_VOUCHER DBOP='INS' >"
        xmlformatpart1 += vbNewLine + "<NUMBER>00005671</NUMBER> - constant"
        xmlformatpart1 += vbNewLine + "<DIVISION>10</DIVISION> - constant"
        xmlformatpart1 += vbNewLine + "<DEPARMENT>1</DEPARMENT> - constant"
        xmlformatpart1 += vbNewLine + "<TYPE>3</TYPE> - constant"
        xmlformatpart1 += vbNewLine + "<NOTES1>23.02.12 GLN HVL </NOTES1> - constant"
        xmlformatpart1 += vbNewLine + "<CREATED_BY>21</CREATED_BY> - constant"
        xmlformatpart1 += vbNewLine + "<TRANSACTIONS>"

        '5076738D120.03.0351861469-0120.02.2012
        TransFormat = vbNewLine + " <TRANSACTION>"
        TransFormat += vbNewLine + "<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant"
        TransFormat += vbNewLine + "<ARP_CODE>VAR1</ARP_CODE>"
        TransFormat += vbNewLine + "<DATE>VAR2</DATE>"
        TransFormat += vbNewLine + "<MODULENR>VAR3</MODULENR>"
        TransFormat += vbNewLine + "<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant"
        TransFormat += vbNewLine + "<AFFECT_RISK>0</AFFECT_RISK> - constant"
        TransFormat += vbNewLine + "</TRANSACTION>"


        xmlformatpart2 = vbNewLine + "</TRANSACTIONS>"
        xmlformatpart2 += vbNewLine + "</BANK_VOUCHER>"
        xmlformatpart2 += vbNewLine + "</BANK_VOUCHERS>"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.TextBox1.Text.Trim.Length <> 38 Then
            MsgBox("Invalid String")
        Else
            Dim final, tf As String
            tf = TransFormat
            final = xmlformatpart1
            tf = tf.Replace("VAR1", (Mid(Me.TextBox1.Text, 9, 11)))
            tf = tf.Replace("VAR2", (Mid(Me.TextBox1.Text, 29, 10)))
            tf = tf.Replace("VAR3", (Mid(Me.TextBox1.Text, 20, 9)))
            final += tf
            final += xmlformatpart2

            Dim sw As New System.IO.StreamWriter("e:\a.xml")
            sw.WriteLine(final)
            sw.Dispose()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim final, tf As String
        final = xmlformatpart1
        For i As Integer = 0 To Me.ListBox1.Items.Count - 1
            If Me.ListBox1.Items.Item(i).Length <> 38 Then
                MsgBox("Invalid String Found!!")
                Exit Sub
            Else

                tf = TransFormat

                tf = tf.Replace("VAR1", (Mid(Me.ListBox1.Items.Item(i), 9, 11)))
                tf = tf.Replace("VAR2", (Mid(Me.ListBox1.Items.Item(i), 29, 10)))
                tf = tf.Replace("VAR3", (Mid(Me.ListBox1.Items.Item(i), 20, 9)))
                final += tf
               
            End If
        Next
        final += xmlformatpart2

        Dim sw As New System.IO.StreamWriter("e:\b.xml")
        sw.WriteLine(final)
        sw.Dispose()
    End Sub
End Class

Also i attach the project file.. Hope it helps you...

Ok a form with one text box two button and one listbox

Public Class Form1
    Dim xmlformatpart1, xmlformatpart2, TransFormat As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        xmlformatpart1 = "'"
        xmlformatpart2 = ""
        TransFormat = ""

        xmlformatpart1 = "<?xml version='1.0' encoding='ISO-8859-9'?>"
        xmlformatpart1 += vbNewLine + "<BANK_VOUCHERS>"
        xmlformatpart1 += vbNewLine + "<BANK_VOUCHER DBOP='INS' >"
        xmlformatpart1 += vbNewLine + "<NUMBER>00005671</NUMBER> - constant"
        xmlformatpart1 += vbNewLine + "<DIVISION>10</DIVISION> - constant"
        xmlformatpart1 += vbNewLine + "<DEPARMENT>1</DEPARMENT> - constant"
        xmlformatpart1 += vbNewLine + "<TYPE>3</TYPE> - constant"
        xmlformatpart1 += vbNewLine + "<NOTES1>23.02.12 GLN HVL </NOTES1> - constant"
        xmlformatpart1 += vbNewLine + "<CREATED_BY>21</CREATED_BY> - constant"
        xmlformatpart1 += vbNewLine + "<TRANSACTIONS>"

        '5076738D120.03.0351861469-0120.02.2012
        TransFormat = vbNewLine + " <TRANSACTION>"
        TransFormat += vbNewLine + "<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE> - constant"
        TransFormat += vbNewLine + "<ARP_CODE>VAR1</ARP_CODE>"
        TransFormat += vbNewLine + "<DATE>VAR2</DATE>"
        TransFormat += vbNewLine + "<MODULENR>VAR3</MODULENR>"
        TransFormat += vbNewLine + "<BANK_PROC_TYPE>2</BANK_PROC_TYPE> - constant"
        TransFormat += vbNewLine + "<AFFECT_RISK>0</AFFECT_RISK> - constant"
        TransFormat += vbNewLine + "</TRANSACTION>"


        xmlformatpart2 = vbNewLine + "</TRANSACTIONS>"
        xmlformatpart2 += vbNewLine + "</BANK_VOUCHER>"
        xmlformatpart2 += vbNewLine + "</BANK_VOUCHERS>"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.TextBox1.Text.Trim.Length <> 38 Then
            MsgBox("Invalid String")
        Else
            Dim final, tf As String
            tf = TransFormat
            final = xmlformatpart1
            tf = tf.Replace("VAR1", (Mid(Me.TextBox1.Text, 9, 11)))
            tf = tf.Replace("VAR2", (Mid(Me.TextBox1.Text, 29, 10)))
            tf = tf.Replace("VAR3", (Mid(Me.TextBox1.Text, 20, 9)))
            final += tf
            final += xmlformatpart2

            Dim sw As New System.IO.StreamWriter("e:\a.xml")
            sw.WriteLine(final)
            sw.Dispose()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim final, tf As String
        final = xmlformatpart1
        For i As Integer = 0 To Me.ListBox1.Items.Count - 1
            If Me.ListBox1.Items.Item(i).Length <> 38 Then
                MsgBox("Invalid String Found!!")
                Exit Sub
            Else

                tf = TransFormat

                tf = tf.Replace("VAR1", (Mid(Me.ListBox1.Items.Item(i), 9, 11)))
                tf = tf.Replace("VAR2", (Mid(Me.ListBox1.Items.Item(i), 29, 10)))
                tf = tf.Replace("VAR3", (Mid(Me.ListBox1.Items.Item(i), 20, 9)))
                final += tf
               
            End If
        Next
        final += xmlformatpart2

        Dim sw As New System.IO.StreamWriter("e:\b.xml")
        sw.WriteLine(final)
        sw.Dispose()
    End Sub
End Class

Also i attach the project file.. Hope it helps you...

thank you so much for your help mate but when i run the project i got error message says nullreferance tf = tf.Replace("VAR1", (Mid(Me.ListBox1.Items.Item(i), 9, 11))) on that line.?

will you post the debugging screenshot ??? Because here no error on my PC..

will you post the debugging screenshot ??? Because here no error on my PC..

Ty so much mate it works, one last question Dim a As String = My.Computer.FileSystem.ReadAllText("c:\txt\*.txt") my text file name is not same all the time so i need to read *.txt file in txt folder but "*." doesnt work ?

You could use an OpenFileDialog to select the file or list all the .txt files in the folder in a list/combo Box and select it that way.

will you post the debugging screenshot ??? Because here no error on my PC..

ok i got problem with that code;

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strFileSize As String = ""
        Dim di As New IO.DirectoryInfo("C:\xml")
        Dim aryFi As IO.FileInfo() = di.GetFiles("*.txt")
        Dim fi As IO.FileInfo
        For Each fi In aryFi
            Dim FilePath As String
            FilePath = fi.FullName
            Dim a As String = My.Computer.FileSystem.ReadAllText(FilePath)
            Dim b As String() = a.Split(vbNewLine)
            ListBox1.Items.AddRange(b)
            Dim Contents = IO.File.ReadAllLines(FilePath)
            Dim HeaderDate As String = ""
            Dim Arp_Code As String = ""
            Dim Debit As String = ""
            For Each line In Contents
                HeaderDate = line.Substring(48, 10)
                Arp_Code = line.Substring(8, 15)
                Debit = line.Substring(76, 17)
                Debit = Debit / 100

                xmlformatpart1 = ""
                xmlformatpart2 = ""
                TransFormat = ""

                xmlformatpart1 = "<?xml version='1.0' encoding='ISO-8859-9'?>"
                xmlformatpart1 += vbNewLine + "<BANK_VOUCHERS>"
                xmlformatpart1 += vbNewLine + "<BANK_VOUCHER DBOP='INS' >"
                xmlformatpart1 += vbNewLine + "<DATE>" & HeaderDate & "</DATE>"
                xmlformatpart1 += vbNewLine + "<NUMBER>00005671</NUMBER>"
                xmlformatpart1 += vbNewLine + "<DIVISION>10</DIVISION>"
                xmlformatpart1 += vbNewLine + "<DEPARMENT>1</DEPARMENT>"
                xmlformatpart1 += vbNewLine + "<TYPE>3</TYPE>"
                xmlformatpart1 += vbNewLine + "<NOTES1>" & HeaderDate & " GLN HVL</NOTES1>"
                xmlformatpart1 += vbNewLine + "<CREATED_BY>21</CREATED_BY>"
                xmlformatpart1 += vbNewLine + "<TRANSACTIONS>"

                TransFormat = vbNewLine + " <TRANSACTION>"
                TransFormat += vbNewLine + "<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE>"
                TransFormat += vbNewLine + "<ARP_CODE>" & Arp_Code & "</ARP_CODE>"
                TransFormat += vbNewLine + "<DATE>" & HeaderDate & "</DATE>"
                TransFormat += vbNewLine + "<MODULENR>7</MODULENR>"
                TransFormat += vbNewLine + "<DEBIT>" & Debit & "</DEBIT>"
                TransFormat += vbNewLine + "<BANK_PROC_TYPE>2</BANK_PROC_TYPE>"
                TransFormat += vbNewLine + "<AFFECT_RISK>0</AFFECT_RISK>"
                TransFormat += vbNewLine + "</TRANSACTION>"

                xmlformatpart2 = vbNewLine + "</TRANSACTIONS>"
                xmlformatpart2 += vbNewLine + "</BANK_VOUCHER>"
                xmlformatpart2 += vbNewLine + "</BANK_VOUCHERS>"
                Dim final, tf As String
                final = xmlformatpart1
                For Each line2 In Contents
                    HeaderDate = line2.Substring(48, 10)
                    Arp_Code = line2.Substring(8, 15)
                    Debit = line2.Substring(76, 17)
                    Debit = Debit / 100
                    final += TransFormat
                Next
                final += xmlformatpart2
                Dim sw As New System.IO.StreamWriter("c:\brs.xml")
                sw.WriteLine(final)
                sw.Dispose()
            Next
        Next

the output like this:

<?xml version='1.0' encoding='ISO-8859-9'?>
<BANK_VOUCHERS>
<BANK_VOUCHER DBOP='INS' >
<DATE>20.02.2012</DATE>
<NUMBER>00005671</NUMBER>
<DIVISION>10</DIVISION>
<DEPARMENT>1</DEPARMENT>
<TYPE>3</TYPE>
<NOTES1>20.02.2012 GLN HVL</NOTES1>
<CREATED_BY>21</CREATED_BY>
<TRANSACTIONS>
<TRANSACTION>
<BANKACC_CODE>102YKB.010.PH1</BANKACC_CODE>
<ARP_CODE>120.03.1186 </ARP_CODE>
<DATE>20.02.2012</DATE>
<MODULENR>7</MODULENR>
<DEBIT>1491,85</DEBIT>
<BANK_PROC_TYPE>2</BANK_PROC_TYPE>
<AFFECT_RISK>0</AFFECT_RISK>
</TRANSACTION>
</TRANSACTIONS>
</BANK_VOUCHER>
</BANK_VOUCHERS>

and here is my txt file :

5076738D120.03.0351 861469-01 20.02.201220.02.201208:34:090000000000012415800000000000124158T1$00007487-01;01 TL 00020X
5076738D120.03.0849 861738-01 20.02.201220.02.201208:34:100000000000002367600000000000023676T1$000075CF-01;01 TL 00791X
5076738D120.03.0850 861737-01 20.02.201220.02.201208:34:090000000000006364700000000000063647T1$000075CE-01;01 TL 00791X
5076738D120.03.0853 861750-01 20.02.201220.02.201208:34:100000000000004418700000000000044187T1$000075DB-01;01 TL 00039X
5076738D120.03.0855 861772-01 20.02.201220.02.201208:34:100000000000018548900000000000185489T1$000075F1-01;01 TL 00166X
5076738D120.03.0947 861821-01 20.02.201220.02.201208:34:110000000000029786300000000000297863T1$0000762C-01;01 TL 00266X
5076738D120.03.0958 861817-01 20.02.201220.02.201208:34:110000000000059914100000000000599141T1$00007627-01;01 TL 00692X
5076738D120.03.0962 861823-01 20.02.201220.02.201208:34:110000000000018396600000000000183966T1$0000762E-01;01 TL 00589X
5076738D120.03.1046 861886-01 20.02.201220.02.201208:34:110000000000061815100000000000618151T1$0000768D-01;01 TL 00692X
5076738D120.03.1089 861911-01 20.02.201220.02.201208:34:120000000000326960400000000003269604T1$000076BE-01;01 TL 00431X
5076738D120.03.1110 861922-01 20.02.201220.02.201208:34:170000000000130276700000000001302767T1$000076D6-01;01 TL 00029X
5076738D120.03.1125 861952-01 20.02.201220.02.201208:34:170000000000005809600000000000058096T1$000076F7-01;01 TL 00668X
5076738D120.03.1166 861990-01 20.02.201220.02.201208:34:180000000000086622900000000000866229T1$0000771A-01;01 TL 00698X
5076738D120.03.1186 861965-01 20.02.201220.02.201208:34:170000000000014918500000000000149185T1$00007704-01;01 TL 00606X

the code only outputs the last record..

Sorry forget to insert

Dim final, tf As String

on line number 63 of my previous code posted...

Sorry forget to insert

Dim final, tf As String

on line number 63 of my previous code posted...

no, your first code working great. But i changed it as you can see, im reading from text file not in listbox and in my modification only the last record writing to xml, not the all lines.. see my last post for the output and modificated code. Thank You.

in your code you use

Dim sw As New System.IO.StreamWriter("c:\brs.xml")
                sw.WriteLine(final)
                sw.Dispose()

for each line. I mean inside a for each loop
so every time it open the xml file write data and close. Texts are not appending they are actually overwritten. so u see only the last line..

i thing you can try

Dim sw As New System.IO.StreamWriter("c:\brs.xml",True)'Use True for appending .... Ok!
                sw.WriteLine(final)
                sw.Dispose()

in your code you use

Dim sw As New System.IO.StreamWriter("c:\brs.xml")
                sw.WriteLine(final)
                sw.Dispose()

for each line. I mean inside a for each loop
so every time it open the xml file write data and close. Texts are not appending they are actually overwritten. so u see only the last line..

i thing you can try

Dim sw As New System.IO.StreamWriter("c:\brs.xml",True)'Use True for appending .... Ok!
                sw.WriteLine(final)
                sw.Dispose()

Thanks for your help it worked!

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.