locked
Convert Text file to Xml file RRS feed

  • Question

  • 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

    5076738D 120.03.0351              861469-01      20.02.2012

    5076738D 120.03.0352              861469-02      21.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>

    • Edited by Begüm ŞEN Wednesday, February 29, 2012 1:03 PM
    Wednesday, February 29, 2012 12:34 PM

Answers

  • Hello,

    Not knowing if you have a delimited text file or not here is an example below which shows how to traverse a tab delimted text file to create a xml structure. For other delimited files see resources at the bottom of the post.

    Hope this is of some assistance.

    Text file (place in same folder as executable) tab delimited, name the file Alarms.txt

    Alarms: Mon Jun 13 10:53:31 2005
          mega	Heater Ch 1 Module heating error	Alarm Enabled
          mega	Heater Sensor error	Alarm Enabled
          mega	Heater Lower Alarm error	Alarm Enabled
           srd	Chem Flow RM2 Not Detected	Alarm Disabled
           srd	Chem off - RM2 detected flow	Alarm Disabled
     system300	Moving to Src Approach with Wafers	Alarm Enabled
     system300	Moving To Destination Position	Alarm Enabled
     system300	Wfrs Detected during Transfers	Alarm Enabled

    Results from code

    <Cleaner>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Wfrs Detected during Transfers</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Moving To Destination Position</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Moving to Src Approach with Wafers</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>srd</system_text>
        <Alarm_text>Chem off - RM2 detected flow</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Disabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>srd</system_text>
        <Alarm_text>Chem Flow RM2 Not Detected</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Disabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Lower Alarm error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Sensor error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Ch 1 Module heating error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
    </Cleaner>

    Form code, place a DataGridView on the form to see the results of code below

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Alarms.txt")
                Dim MyAlarms = _
                   <Cleaner>
                   </Cleaner>
                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {vbTab}
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        If currentRow(0).StartsWith("Alarms:") Then
                        Else
                            Dim Item = _
                                  <CleanerAlarms>
                                      <system_text><%= currentRow(0) %></system_text>
                                      <Alarm_text><%= currentRow(1) %></Alarm_text>
                                      <Alarm_type>Alarm</Alarm_type>
                                      <STATE><%= currentRow(2).Replace("Alarm ", "") %></STATE>
                                  </CleanerAlarms>
                            MyAlarms.AddFirst(Item)
                        End If
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                    End Try
                End While
                Console.WriteLine(MyAlarms.ToString)
                Dim Doc As New XDocument
                Doc = XDocument.Parse(MyAlarms.ToString)
                DataGridView1.DataSource =
                    (
                        From t In Doc...<CleanerAlarms>
                        Select System = t.<system_text>.Value,
                        Alarm = t.<Alarm_text>.Value, _
                        State = t.<STATE>.Value
                        ).ToList
            End Using
        End Sub
    End Class

    Resources

    TextFieldParser

    How to: Embed Expressions in XML Literals (Visual Basic)


    KSG

    Wednesday, February 29, 2012 2:45 PM

All replies

  • Hello,

    Not knowing if you have a delimited text file or not here is an example below which shows how to traverse a tab delimted text file to create a xml structure. For other delimited files see resources at the bottom of the post.

    Hope this is of some assistance.

    Text file (place in same folder as executable) tab delimited, name the file Alarms.txt

    Alarms: Mon Jun 13 10:53:31 2005
          mega	Heater Ch 1 Module heating error	Alarm Enabled
          mega	Heater Sensor error	Alarm Enabled
          mega	Heater Lower Alarm error	Alarm Enabled
           srd	Chem Flow RM2 Not Detected	Alarm Disabled
           srd	Chem off - RM2 detected flow	Alarm Disabled
     system300	Moving to Src Approach with Wafers	Alarm Enabled
     system300	Moving To Destination Position	Alarm Enabled
     system300	Wfrs Detected during Transfers	Alarm Enabled

    Results from code

    <Cleaner>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Wfrs Detected during Transfers</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Moving To Destination Position</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>system300</system_text>
        <Alarm_text>Moving to Src Approach with Wafers</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>srd</system_text>
        <Alarm_text>Chem off - RM2 detected flow</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Disabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>srd</system_text>
        <Alarm_text>Chem Flow RM2 Not Detected</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Disabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Lower Alarm error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Sensor error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
      <CleanerAlarms>
        <system_text>mega</system_text>
        <Alarm_text>Heater Ch 1 Module heating error</Alarm_text>
        <Alarm_type>Alarm</Alarm_type>
        <STATE>Enabled</STATE>
      </CleanerAlarms>
    </Cleaner>

    Form code, place a DataGridView on the form to see the results of code below

    Public Class Form1
        Private Sub Form1_Load(
            ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Alarms.txt")
                Dim MyAlarms = _
                   <Cleaner>
                   </Cleaner>
                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {vbTab}
                Dim currentRow As String()
                While Not MyReader.EndOfData
                    Try
                        currentRow = MyReader.ReadFields()
                        If currentRow(0).StartsWith("Alarms:") Then
                        Else
                            Dim Item = _
                                  <CleanerAlarms>
                                      <system_text><%= currentRow(0) %></system_text>
                                      <Alarm_text><%= currentRow(1) %></Alarm_text>
                                      <Alarm_type>Alarm</Alarm_type>
                                      <STATE><%= currentRow(2).Replace("Alarm ", "") %></STATE>
                                  </CleanerAlarms>
                            MyAlarms.AddFirst(Item)
                        End If
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        MsgBox("Line " & ex.Message & " is invalid.  Skipping")
                    End Try
                End While
                Console.WriteLine(MyAlarms.ToString)
                Dim Doc As New XDocument
                Doc = XDocument.Parse(MyAlarms.ToString)
                DataGridView1.DataSource =
                    (
                        From t In Doc...<CleanerAlarms>
                        Select System = t.<system_text>.Value,
                        Alarm = t.<Alarm_text>.Value, _
                        State = t.<STATE>.Value
                        ).ToList
            End Using
        End Sub
    End Class

    Resources

    TextFieldParser

    How to: Embed Expressions in XML Literals (Visual Basic)


    KSG

    Wednesday, February 29, 2012 2:45 PM
  • Hi kevin ,

    i forgot to say my txt file actually like this ;

    5076738D120.03.0351861469-0120.02.2012
    5076738D120.03.0352861469-0221.02.2012

    no "," or space character in it.. so i have to use substring i think..

    Wednesday, February 29, 2012 2:53 PM
  • Hi kevin ,

    i forgot to say my txt file actually like this ;

    5076738D120.03.0351861469-0120.02.2012
    5076738D120.03.0352861469-0221.02.2012

    no "," or space character in it.. so i have to use substring i think..

    Yes you will need to use string functions as shown below but with better variable names instead of P1 and P2 etc.

    Dim FileName As String = "MyFileName"
    Dim Contents = IO.File.ReadAllLines(FileName)
    Dim P1 As String = ""
    Dim P2 As String = ""
    For Each line In Contents
        P1 = line.Substring(0, 8)
        P2 = line.Substring(8, 11)
        '
        ' Continue parsing out fields
        ' Place into xml structure as shown in prior example
        '
    Next


    KSG

    Wednesday, February 29, 2012 3:05 PM