howexg
howexg

Reputation: 45

How to convert a fixed width file to XML file?

I have a fixed width text file to convert to XML. Can you tell me how to make it in detail?

My sample text looks like this...

9621120080828200808290001084713CENTRAL TIONG NAM WAREHOUSE        086014286100501330071881000020000000000000090002PC       20.000       20.000 81E0       600.000         20.000           PC UB                      
9621120080828200808290001084713CENTRAL TIONG NAM WAREHOUSE        086014286100501330071881900001000000000000011131PC     1000.000    24000.000 8100     13680.000       1920.000 47/2008   CRTUB  00100000000003346495

from the first line I want to convert text file based on length..

Note : first two lines is considered as one record.....

I want the output like below

<FileNo>96211<FileNo>
<ShipmentCreationDate>20080828<ShipmentCreationDate>
<DeliveryDate>20080829<DeliveryDate>

Thanks in advance!

Upvotes: 3

Views: 2284

Answers (2)

kasten
kasten

Reputation: 628

If you have no control over the outputting Application you will have to write a converter tool.

Take your preferend language and matching XML-Lib and write a small application which parses the data and writes it as XML.

Upvotes: 0

marc_s
marc_s

Reputation: 754993

I would urge you to have a look at the FileHelpers library.

It's a C# library that allows you to import fixed-width or delimited files into C# very quickly.

You can then easily turn around and spit out those records as XML from your C# - no problem at all!

In detail:

  • using FileHelpers, you would define a class that represents your data (one line of it)
  • FileHelpers then imports the contents of a file into an array of objects of that class
  • using XmlSerializer, you can then easily serialize those objects back out to XML

Should be about 50 lines or less of code :-)

Upvotes: 4

Related Questions