Link to home
Start Free TrialLog in
Avatar of MiracleByDesign
MiracleByDesign

asked on

How to create a fixed length file with an XML document as your source

Hello Experts,
I need to create a fixed length file from my xml data source.  I do not have any idea where to start or what programming language is the best.  I was thinking C#.NET but the file is large with a lot of fields and I did not want to have to type it all out.  Needing a quick solution today if possible.

Thanks!
Avatar of MiracleByDesign
MiracleByDesign

ASKER

To convert a text-based XML file into a binary fixed-length data file would take a bit of work.

Normally, you'd use an XML Style Sheet Transform (XSLT) to convert from XML to another format.  However, most of the "other format" are text-based (such as HTML).  Yes, there is a branch of XSLT that deals with binary ouput called Formatting Objects (XSL-FO), but again... they are designed to output to some known "mostly"-text based format (like PDF).

So, that pretty much means that you're gonna havta write an application to perform this task. If you have some experience with C#, it's not difficult to parse the incomming XML file into a custom class.  Then you'd write code to output the information in the class to a binary file (padding as appropriate to meet the fixed length requirements).
Hi Graye,

Thank you for your insight.  Could you give me a little coding example to get started?  I have not used XSLT at all,  I just need a little start and then I can take it from there!

Thanks!
Miracle By Design
Also the length requirements are in the xml document, anyway we could use this?
No, I'm suggesting that you NOT use XSLT... and instead write a custom application (perhaps in C#) that will read the XML input and then output binary.

Here is a "classic" example for reading an XML file.
http://support.microsoft.com/kb/307548

Here is a bit more complex example using an custom class that has been configured for serialization:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Ok, I have been working on this for about 4 days now and I attempted to look at the links you provided when I googled it.  I guess my question is, since this file has a large number of fields and I have the start position and the length, how to I access this information in order to create my flat file?  I do not want to use the LPad or RPad when I have the data already in the xml document.  This I could not get to work correctly.  Could you provide me with a very small sample of code with just a few fields?  Thanks
That example at the bottom of the 2nd link is a pretty good place to start.  It shows how to "import" the contents of an XML file into a custom class that you create.  You can choose to keep the Start and Length attributes when you "import" the file.

Then, after you've got all of the data, it'd be somewhat trival to output the contents of the class to a binary file (yes... using LPad-equivalent padding for the text portions).

But, before we get that far, can you show us a sample of what the final output looks like (I've been assuming all along, that this is a binary file format).
Sure...the attached is a sample of what I am trying to create. EE-Sample-File.txt
ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Graye,

The numeric values are already padded in the SQL table which populates the XML document.  The space padding is not though so I would need to do that with the XSL formatting.  I am going to the website you providing now and looking up XSL.  Thank you again for your help!
Hi Graye,

I have looked at the website and I am ready for your examples using XSL if you are still available to help me.  I need to get this file produced tonight if possible.

Thanks,
Miracle By Design
Wow... no pressure

OK, send me a small chunk of the XML file that has actual (or fake) data
Sorry for the pressure.  I have been working on this for a week now by myself.  The xml file, scheme and sample data are attached above.  If you do not have time, I understand...no worries!
But that XML file didn't have any data....
There are a lot of fields to supply fake data and the real data as EIN and SSN in it.  Can you provide an example of using xsl without the data?
Most of the name fields in the xml file are pretty easy to figure out the data that goes in there.  For example, name (first, last, middle), delivery address (ex suite 200) location address(1234 s anyplace), EIN-10-99999999, Wages, etc.  This file is to electronically submit W2s so I am behind the gun.  I created one file but it was not in the XML format so the file was rejected.  At this point I am just cutting and pasting the data because I have to get this out today.  I appreciate your help, you if you post a small code sample of how to use XSL to accomplish this task, I will award you the 500 points.  Thanks again!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The URL I provided is the coding tool needed to get the job completed.