?
Solved

Using C# .Net 2.0 to parse a text file.

Posted on 2008-10-23
6
Medium Priority
?
1,088 Views
Last Modified: 2013-12-17
Looking for some advice on how to go about parsing data out of a text file - just need enough help to get started down the right road.  Or if there's an open source library available that'll do the job...I have no interest in re-inventing the wheel!

The text file I have to work with is like the one below; I need to extract a couple bits of information out of the header (like invoice number, which warehouse, customer PO) and information out of the line items, and my intent is to populate a strongly typed dataset.

I think I could hard-code a parsing routine pretty easily, but I was hoping to have something like an XML file with parsing rules since the format may change in the future, and the format could be different from warehouse to warehouse - and with the parsing rules in an external XML file it's easy to change, and I could have different XML files to accomodate more than one format (especially if it ends up being someone not familiar with programming).
12:04:18                                                             23 OCT 2008
                     * * *   P I C K    T I C K E T   * * *        PAGE  1 OF  2
INVOICE NO. 123456            WAREHOUSE 12-A
    REF NO. 123456
 
CUST NO. 123456      PH                SALE TYPE WHLS/C
 
      SOLD TO:                            SHIP TO:
      DOE,JOHN                            DOE,JOHN
      123 MAIN ST                         123 MAIN ST
      METROPOLIS, US 12345-1234           METROPOLIS, US 12345-1234
 
 
PO NO. CUST-PO-HERE  SHIP VIA SHIP-METHOD-HERE
PC PART-NO................. Q.O. BIN..... O.H. DESC..............
PN                   123456    1      A12    5 TEST PART
PN                   789012    1      B34    5 ANOTHER TEST PART
 
 
--- Snip some unknown number of line items on page 1 --
 
12:04:18                                                             23 OCT 2008
                     * * *   P I C K    T I C K E T   * * *        PAGE  1 OF  2
INVOICE NO. 123456            WAREHOUSE 12-A
    REF NO. 123456
 
CUST NO. 123456      PH                SALE TYPE WHLS/C
 
      SOLD TO:                            SHIP TO:
      DOE,JOHN                            DOE,JOHN
      123 MAIN ST                         123 MAIN ST
      METROPOLIS, US 12345-1234           METROPOLIS, US 12345-1234
 
 
PO NO. CUST-PO-HERE       SHIP VIA SHIP-METHOD-HERE
PC PART-NO................. Q.O. BIN..... O.H. DESC..............
PN                   345678    1      C56      TEST
PN                   901234    1      D78      TEST
                             TOT PC    4

Open in new window

0
Comment
Question by:Todd Gerbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22790316
Seems to me that that file structure is unique, and you will have to create your own parsing routine
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22790340
looks pretty painful to generate something that will read an XML file to parse this, rather then write the parser itself
0
 
LVL 33

Author Comment

by:Todd Gerbert
ID: 22792899
Agreed.

But, if the vendor supplying this data decides to change their format it would be much easier to just adjust my parsing rules XML file, as opposed to re-writing code - especially if I happen to not be at the same company (I don't want to leave solutions behind that they can't support without me).  Plus, then I'd have re-usable code.
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 0 total points
ID: 22922314
I haven't found a simple solution, and I'm slightly disinclined to write my own grammar/compiler, so I've decided to go ahead and code a simple parsing routine in C#, but I've left that code in an external text file that's compiled at run-time - at least this way I can make adjustments fast, and maybe someone else besides me will stand a chance of tweaking it if need-be.
0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22923685
gl in the future
0
 
LVL 33

Author Comment

by:Todd Gerbert
ID: 22923788
Care to elaborate? ;)

(Sorry, only GL I know is graphics lib OpenGL)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question