Parsing a stream of data

Posted on 2005-04-08
Last Modified: 2010-04-16
I have a program that is reading in a stream of data every 2 seconds or so.

I want to accumulate the data into a string variable...then work on it at my leisure.

The reason for this is the data coming in is not a "complete" record.

Sometimes it may take 2 or 3 readings to get a full record.

I only want to parse full records.  If I try and parse partial records my tokenizer will fail.

Full records start with &&   and  end with  !!

Question by:knowlton
    LVL 14

    Accepted Solution

    This example is based upon reading a textfile but also can be used for other streams. Since I have no idea about your record layout. I just assumed that all records between the "&&" and "!!" are put in a string seperated with "\n".

    StreamReader sr = new StreamReader(configfile);
    string line = null;
    string record = null;
    while((line = sr.ReadLine()) != null)
    // Start of record
    if(line.StartsWith("&&")) {
     record = line;
    // End of record
    else if(line.StartsWith("!!")) {
     //do something with the record
    // Record value
    else {
    record += line;
    LVL 5

    Author Comment

    This looks good....

    One quick questin on CR  (carriage return)   versus  LF  (line feed)

    What does each one do?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now