How to convert a text file with varying record layout to XML?

327 Views Asked by At

I have a text file (generated from a COBOL legacy system) that has a varying record layout. It is analogous to this:

http://www.stylusstudio.com/varying_record_layout.html

What I want to do is parse that text file using C# a get an output in XML format and then load its content to a database. (In few words make ETL operation).

How can I accomplish this task using C# and handle an XML API? Some suggestions, tips, &c.?

1

There are 1 best solutions below

0
On

You might try something like this:

  1. Write code to manually create a DataSet with
  • DataTables that mimic each type of heterogenous records. OR
  • contains a single DataTable that accommodates all of the heterogenous data.
  1. Parse the records and and distribute them in the the appropriate DataTables in your DataSet

  2. Serialize the DataSet using code similar to the code below.

Then, you can easily load the data directly into a DataSet if you need to manipulate it.

private void SerializeDataSet(DataSet ds, string filename){
    XmlSerializer ser = new XmlSerializer(typeof(DataSet));    
    TextWriter writer = new StreamWriter(filename);
    ser.Serialize(writer, ds);
    writer.Close();
}

Here is a link to the MSDN documentation for XML Serialization.