How To Parse A Data File?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmaclam
    New Member
    • Mar 2010
    • 6

    How To Parse A Data File?

    I am just learning to code in C# and need to parse a file that is in the following format:

    [VOR]
    YMS 114.500 N044.08.35.400 W080.08.46.200
    YTP 116.550 N043.40.18.00 W079.39.51.000
    YWT 115.000 N043.27.31.200 W080.22.45.600
    YSO 117.350 N044.14.19.200 W079.10.30.000
    YYZ 112.150 N043.39.28.800 W079.37.54.000

    [NDB]
    OO 391 N043.55.18.000 W078.54.18.000
    KZ 248 N043.56.01.000 W079.19.44.999
    TZ 257 N043.36.46.000 W079.23.07.999
    ZDH 385 N043.44.17.000 W079.34.11.000
    ZLP 341 N043.37.41.000 W079.43.55.000
    ZTO 403.000 N043.44.19.671 W079.42.09.868
    ZYZ 368.000 N043.37.10.222 W079.32.56.396

    [Next Section]
    ....

    The amount of data in each section is different in every file.

    I need a way to get data from one section at a time. I know how to read and split the lines but cant seem to figure out how to only grab data from one section.

    Any help would be great.
    Thanks
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    search for the line which contains ' [ ' and ' ] ' then start a new reading process

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Or search for the blank line between the blocks

      Comment

      • dmaclam
        New Member
        • Mar 2010
        • 6

        #4
        So I have decided to parse the entire file using 2 passes.
        The first pass it to get the number of data fields in each section and the second pass will actually populate the data into their respective arrays.

        here is the code for the first pass:

        Code:
        public void OpenSector(string strFile)
                {
                    if (File.Exists(strFile))
                    {
                        using (StreamReader re = File.OpenText(strFile))
                        {
                            int lines=0;//For Line Counting
                            int comments = 0;//Counts Comment Lines
                            int blanks = 0;//Counts Blank Lines
                            int sectioncount = 0;//Counts Sections In Sector "[...]"
                            int colorcount = 0;//Counts Color Definitions #define AQUA 16776960
                            int infocount = 0;//Counts Info Lines
                            int airportcount = 0;//Counts Airports
                            int runwaycount = 0;//Counts Runsways
                            int vorcount = 0;//Counts VORs
                            int ndbcount = 0;//Counts NDBs
                            int fixcount = 0;//Counts Fixes
                            int labelcount = 0;//Counts Labels
                            int lowairwaycount = 0;//Counts Low Airways
                            int highairwaycount = 0;//Counts High Airways
                            int geocount = 0;//Counts Geo Data
                            int artcclow = 0;//Counts artcc lower boundries
                            int artcchigh = 0;//Counts artcc high boundries
                            int starcount = 0;//Counts Stars
                            int sidcount = 0;//Counts Sids
        
                            String input;
                            string section = "";
        
                            while ((input=re.ReadLine())!=null) 
                            {
                                lines = lines + 1;
                                if (input == "")
                                {
                                    blanks = blanks + 1;
                                }
                                else if (input.Substring(0, 1) == ";")
                                {
                                    //Is a comment
                                    comments = comments + 1;
                                }
                                else if (input.Substring(0, 1) == "#")
                                {
                                    //Is a comment
                                    colorcount = colorcount + 1;
                                }
                                else if (input.Substring(0, 1) == "[")
                                {
                                    sectioncount = sectioncount + 1;
                                    section = input.Substring(0, input.IndexOf("]") + 1);
                                }
                                else
                                {
                                    switch (section.ToUpper())
                                    {
                                        case "[INFO]":
                                            infocount = infocount + 1;
                                            break;
                                        case "[AIRPORT]":
                                            airportcount = airportcount + 1;
                                            break;
                                        case "[RUNWAY]":
                                            runwaycount = runwaycount + 1;
                                            break;
                                        case "[VOR]":
                                            vorcount = vorcount + 1;
                                            break;
                                        case "[NDB]":
                                            ndbcount = ndbcount + 1;
                                            break;
                                        case "[FIXES]":
                                            fixcount = fixcount + 1;
                                            break;
                                        case "[LABELS]":
                                            labelcount = labelcount + 1;
                                            break;
                                        case "[LOW AIRWAY]":
                                            lowairwaycount = lowairwaycount + 1;
                                            break;
                                        case "[HIGH AIRWAY]":
                                            highairwaycount = highairwaycount + 1;
                                            break;
                                        case "[GEO]":
                                            geocount = geocount + 1;
                                            break;
                                        case "[ARTCC LOW]":
                                            
                                            artcclow = artcclow + 1;
                                            break;
                                        case "[ARTCC HIGH]":
                                           
                                            artcchigh = artcchigh + 1;
                                            break;
                                        case "[STAR]":
                                           
                                            starcount = starcount + 1;
                                            break;
                                        case "[SID]":
                                           
                                            sidcount = sidcount + 1;
                                            break;
                                    }
                                }
                            }
                    
                            //Reading Done
                            re.Close();//Close Stream Reader
        }

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          By defining all your variables within the method none of them available one the method completes and returns.

          You aren't doing anything within the method with the counts.
          So what happens next? It is almost as if all this processing gets thrown away.

          Comment

          • dmaclam
            New Member
            • Mar 2010
            • 6

            #6
            as of right now the method does nothing but count the amount of data in each section.
            i am currently writing the second pass of the file where i will setup array for all the data. I am simple using all the count values to define the array size. The arrays will return the data to the main form.

            string[] strInfo = new string[infocount];

            i will post the second pass once i have it written

            :D

            Comment

            • dmaclam
              New Member
              • Mar 2010
              • 6

              #7
              Here is the final class. It calculates how many data fields in each section. (completed in the first pass). Then puts the data into the appropriate public string arrays.(complet ed in the second pass)

              Code:
                class SectorFileParser
                  {
                      //Declare Sector File Data Arrays
                      public string[] strColorName,strColorValue,strInfo;
                      public string[] strAirportID, strAirportFreq, strAirportLat, strAirportLon, strAirportClass;
                      public string[] strRunwayID, strRunwayHeadings, strRunwayStartLat, strRunwayStartLon, strRunwayEndLat, strRunwayEndLon;
                      public string[] strVorID, strVorFreq, strVorLat, strVorLon;
                      public string[] strNDBID, strNDBFreq, strNDBLat, strNDBLon;
                      public string[] strFixID, strFixLat, strFixLon;
                      public string[] strLabelID, strLabelLat, strLabelLon, strLabelColor;
                      public string[] strLowAirwayID, strLowAirwayStartLat, strLowAirwayStartLon, strLowAirwayEndLat, strLowAirwayEndLon;
                      public string[] strHighAirwayID, strHighAirwayStartLat, strHighAirwayStartLon, strHighAirwayEndLat, strHighAirwayEndLon;
                      public string[] strGeoStartLat, strGeoStartLon, strGeoEndLat, strGeoEndLon, strGeoColor;
                      public string[] strArtccLowID, strArtccLowStartLat, strArtccLowStartLon, strArtccLowEndLat, strArtccLowEndLon;
                      public string[] strArtccHighID, strArtccHighStartLat, strArtccHighStartLon, strArtccHighEndLat, strArtccHighEndLon;
                      public string[] strStarID, strStarStartLat, strStarStartLon, strStarEndLat, strStarEndLon ,strStarColor ;
                      public string[] strSIDID, strSIDStartLat, strSIDStartLon, strSIDEndLat, strSIDEndLon, strSIDColor;
              
                      public void OpenSector(string strFile)
                      {
                          if (File.Exists(strFile))
                          {
                             //Start Pass 1
                              using (StreamReader re = File.OpenText(strFile))
                              {
                                  int lines=0;//For Line Counting
                                  int comments = 0;//Counts Comment Lines
                                  int blanks = 0;//Counts Blank Lines
                                  int sectioncount = 0;//Counts Sections In Sector "[...]"
                                  int colorcount = 0;//Counts Color Definitions #define AQUA 16776960
                                  int infocount = 0;//Counts Info Lines
                                  int airportcount = 0;//Counts Airports
                                  int runwaycount = 0;//Counts Runsways
                                  int vorcount = 0;//Counts VORs
                                  int ndbcount = 0;//Counts NDBs
                                  int fixcount = 0;//Counts Fixes
                                  int labelcount = 0;//Counts Labels
                                  int lowairwaycount = 0;//Counts Low Airways
                                  int highairwaycount = 0;//Counts High Airways
                                  int geocount = 0;//Counts Geo Data
                                  int artcclow = 0;//Counts artcc lower boundries
                                  int artcchigh = 0;//Counts artcc high boundries
                                  int starcount = 0;//Counts Stars
                                  int sidcount = 0;//Counts Sids
              
                                  String input;
                                  string section = "";
              
                                  while ((input=re.ReadLine())!=null) 
                                  {
                                      lines = lines + 1;
                                      if (input == "")
                                      {
                                          blanks = blanks + 1;
                                      }
                                      else if (input.Substring(0, 1) == ";")
                                      {
                                          //Is a comment
                                          comments = comments + 1;
                                      }
                                      else if (input.Substring(0, 1) == "#")
                                      {
                                          //Is a comment
                                          colorcount = colorcount + 1;
                                      }
                                      else if (input.Substring(0, 1) == "[")
                                      {
                                          sectioncount = sectioncount + 1;
                                          section = input.Substring(0, input.IndexOf("]") + 1);
                                      }
                                      else
                                      {
                                          switch (section.ToUpper())
                                          {
                                              case "[INFO]":
                                                  infocount = infocount + 1;
                                                  break;
                                              case "[AIRPORT]":
                                                  airportcount = airportcount + 1;
                                                  break;
                                              case "[RUNWAY]":
                                                  runwaycount = runwaycount + 1;
                                                  break;
                                              case "[VOR]":
                                                  vorcount = vorcount + 1;
                                                  break;
                                              case "[NDB]":
                                                  ndbcount = ndbcount + 1;
                                                  break;
                                              case "[FIXES]":
                                                  fixcount = fixcount + 1;
                                                  break;
                                              case "[LABELS]":
                                                  labelcount = labelcount + 1;
                                                  break;
                                              case "[LOW AIRWAY]":
                                                  lowairwaycount = lowairwaycount + 1;
                                                  break;
                                              case "[HIGH AIRWAY]":
                                                  highairwaycount = highairwaycount + 1;
                                                  break;
                                              case "[GEO]":
                                                  geocount = geocount + 1;
                                                  break;
                                              case "[ARTCC LOW]":
                                                  //Identify Names Here To Get Accurate Count
                                                  artcclow = artcclow + 1;
                                                  break;
                                              case "[ARTCC HIGH]":
                                                  //Identify Names Here To Get Accurate Count
                                                  artcchigh = artcchigh + 1;
                                                  break;
                                              case "[STAR]":
                                                  //Identify Names Here To Get Accurate Count
                                                  starcount = starcount + 1;
                                                  break;
                                              case "[SID]":
                                                  //Identify Names Here To Get Accurate Count
                                                  sidcount = sidcount + 1;
                                                  break;
                                          }
                                      }
                                  }
                          
                                  //Pass 1 Complete
                                  re.Close();//Close Stream Reader
                                
              
                                  //Start Pass 2
                                  
                                  //Setup Color Arrays
                                  int tempcolor = -1;
                                  strColorName  = new string[colorcount];
                                  strColorValue = new string[colorcount];
              
                                  //Setup Info Array
                                  int tempinfo = -1;
                                  strInfo = new string[infocount];
              
                                  //Setup Runaway Arrays
                                  int temprunways = -1;
                                  strRunwayID = new string[runwaycount];
                                  strRunwayHeadings = new string[runwaycount];
                                  strRunwayStartLat = new string[runwaycount];
                                  strRunwayStartLon = new string[runwaycount];
                                  strRunwayEndLat = new string[runwaycount];
                                  strRunwayEndLon = new string[runwaycount];
              
                                  //Setup Airport Arrays
                                  int tempairport = -1;
                                  strAirportID = new string[airportcount];
                                  strAirportFreq = new string[airportcount];
                                  strAirportLat = new string[airportcount];
                                  strAirportLon = new string[airportcount];
                                  strAirportClass = new string[airportcount];
              
                                  //Setup VOR Arrays
                                  int tempVor = -1;
                                  strVorID = new string[vorcount];
                                  strVorFreq = new string[vorcount];
                                  strVorLat = new string[vorcount];
                                  strVorLon = new string[vorcount];
              
                                  //Setup NDB Arrays
                                  int tempNDB = -1;
                                  strNDBID = new string[ndbcount];
                                  strNDBFreq = new string[ndbcount];
                                  strNDBLat = new string[ndbcount];
                                  strNDBLon = new string[ndbcount];
              
                                  //Setup Fix Arrays
                                  int tempFix = -1;
                                  strFixID = new string[fixcount];
                                  strFixLat = new string[fixcount];
                                  strFixLon = new string[fixcount];
              
                                  //Setup Label Arrays
                                  int tempLabel = -1;
                                  strLabelID=new string[labelcount];
                                  strLabelLat = new string[labelcount];
                                  strLabelLon = new string[labelcount];
                                  strLabelColor = new string[labelcount]; 
              
                                  //Setup Low Airway Arrays
                                  int tempLowAirways = -1;
                                  strLowAirwayID = new string[lowairwaycount];
                                  strLowAirwayStartLat = new string[lowairwaycount];
                                  strLowAirwayStartLon = new string[lowairwaycount];
                                  strLowAirwayEndLat = new string[lowairwaycount];
                                  strLowAirwayEndLon = new string[lowairwaycount];
              
                                  //Setup High Airway Arrays
                                  int tempHighAirways = -1;
                                  strHighAirwayID = new string[highairwaycount];
                                  strHighAirwayStartLat = new string[highairwaycount];
                                  strHighAirwayStartLon = new string[highairwaycount];
                                  strHighAirwayEndLat = new string[highairwaycount];
                                  strHighAirwayEndLon = new string[highairwaycount];
              
                                  //Setup Geo Data Arrays
                                  int tempGeo = -1;
                                  strGeoStartLat = new string[geocount];
                                  strGeoStartLon = new string[geocount];
                                  strGeoEndLat = new string[geocount];
                                  strGeoEndLon = new string[geocount];
                                  strGeoColor = new string[geocount];
              
                                  //Setup Low ARTCC Arrays
                                  int tempARTCCLow = -1;
                                  strArtccLowID = new string[artcclow];
                                  strArtccLowStartLat = new string[artcclow];
                                  strArtccLowStartLon = new string[artcclow];
                                  strArtccLowEndLat = new string[artcclow];
                                  strArtccLowEndLon = new string[artcclow];
              
                                  //Setup High ARTCC Arrays
                                  int tempARTCCHigh = -1;
                                  strArtccHighID = new string[artcchigh];
                                  strArtccHighStartLat = new string[artcchigh];
                                  strArtccHighStartLon = new string[artcchigh];
                                  strArtccHighEndLat = new string[artcchigh];
                                  strArtccHighEndLon = new string[artcchigh];
              
                                  //Setup Star Arrays
                                  int tempStar = -1;
                                  strStarID=new string[starcount];
                                  strStarStartLat = new string[starcount];
                                  strStarStartLon = new string[starcount];
                                  strStarEndLat = new string[starcount];
                                  strStarEndLon = new string[starcount];
                                  strStarColor = new string[starcount];
              
                                  //Setup Sid Arrays
                                  int tempSid = -1;
                                  strSIDID = new string[sidcount];
                                  strSIDStartLat = new string[sidcount];
                                  strSIDStartLon = new string[sidcount];
                                  strSIDEndLat = new string[sidcount];
                                  strSIDEndLon = new string[sidcount];
                                  strSIDColor = new string[sidcount];
              
                                  //Read The Data
                                  using (StreamReader re2 = File.OpenText(strFile))
                                  {
                                      while ((input = re2.ReadLine()) != null)
                                      {
                                          
                                          if (input == "")
                                          {
                                             //Blank Line So Ignore It
                                          }
                                          else if (input.Substring(0, 1) == ";")
                                          {
                                              //Is a comment so Ignore it
                                              
                                          }
                                          else if (input.Substring(0, 1) == "#")
                                          {
                                              //Load Colors Into Array
                                              tempcolor = tempcolor + 1;
                                              string[] parsedcolor = input.Split(new Char [] {' '});
                                              strColorName[tempcolor] = parsedcolor[1];
                                              strColorValue[tempcolor] = parsedcolor[2];
                                          }
                                          else if (input.Substring(0, 1) == "[")
                                          {
                                              sectioncount = sectioncount + 1;
                                              section = input.Substring(0, input.IndexOf("]") + 1);
                                          }
                                          else
                                          {
                                              //Steralize Comments Off Ends Of Data 
                                              if (input.IndexOf(";") > 0)
                                              {
                                                  input = input.Substring(0, input.IndexOf(";" ));
                                              }
                                              switch (section.ToUpper())
                                              {
                                                  case "[INFO]":
                                                      tempinfo = tempinfo + 1;
                                                      strInfo[tempinfo] = input;
                                                      break;
                                                  case "[AIRPORT]":
                                                      tempairport = tempairport + 1;
                                                      string[] parsedairport = input.Split(new Char[] { ' ' });
                                                      strAirportID[tempairport] = parsedairport[0];
                                                      strAirportFreq[tempairport] = parsedairport[1];
                                                      strAirportLat[tempairport] = parsedairport[2];
                                                      strAirportLon[tempairport] = parsedairport[3];
                                                      strAirportClass[tempairport] = parsedairport[4];
                                                      break;
                                                  case "[RUNWAY]":
                                                      temprunways = temprunways + 1;
                                                      string[] parsedrunways = input.Split(new Char[] { ' ' });
                                                      strRunwayID[temprunways] = parsedrunways[0] + "/" + parsedrunways[1];
                                                      strRunwayHeadings[temprunways] = parsedrunways[2] + "/" + parsedrunways[3];
                                                      strRunwayStartLat[temprunways] = parsedrunways[4];
                                                      strRunwayStartLon[temprunways] = parsedrunways[5];
                                                      strRunwayEndLat[temprunways] = parsedrunways[6];
                                                      strRunwayEndLon[temprunways] = parsedrunways[7];
                                                      break;
                                                  case "[VOR]":
                                                      tempVor = tempVor + 1;
                                                      string[] parsedVORs = input.Split(new Char[] { ' ' });
                                                      strVorID[tempVor] = parsedVORs[0];
                                                      strVorFreq[tempVor] = parsedVORs[1];
                                                      strVorLat[tempVor] = parsedVORs[2];
                                                      strVorLon[tempVor] = parsedVORs[3];
                                                      break;
                                                  case "[NDB]":
                                                      tempNDB = tempNDB + 1;
                                                      string[] parsedNDBs = input.Split(new Char[] { ' ' });
                                                      strNDBID[tempNDB] = parsedNDBs[0];
                                                      strNDBFreq[tempNDB] = parsedNDBs[1];
                                                      strNDBLat[tempNDB] = parsedNDBs[2];
                                                      strNDBLon[tempNDB] = parsedNDBs[3];
                                                      break;
                                                  case "[FIXES]":
                                                      tempFix = tempFix + 1;
                                                      string[] parsedfix=input.Split(new Char[] { ' ' });
                                                      strFixID[tempFix] = parsedfix[0];
                                                      strFixLat[tempFix] = parsedfix[1];
                                                      strFixLon[tempFix] = parsedfix[2];
                                                      break;
                                                  case "[LABELS]":
                                                      tempLabel = tempLabel + 1;
                                                      string[] parsedLabel = input.Split(new Char[] { ' ' });
                                                      strLabelID[tempLabel] = parsedLabel[0].Substring(1, parsedLabel[0].Length - 2);
                                                      strLabelLat[tempLabel] = parsedLabel[1];
                                                      strLabelLon[tempLabel] = parsedLabel[2];
                                                      strLabelColor[tempLabel] = parsedLabel[3];
                                                      break;
                                                  case "[LOW AIRWAY]":
                                                      tempLowAirways = tempLowAirways + 1;
                                                      string[] parsedLowAirways= input.Split(new Char[] { ' ' });
                                                      strLowAirwayID[tempLowAirways] = parsedLowAirways[0];
                                                      strLowAirwayStartLat[tempLowAirways] = parsedLowAirways[1];
                                                      strLowAirwayStartLon[tempLowAirways] = parsedLowAirways[2];
                                                      strLowAirwayEndLat[tempLowAirways] = parsedLowAirways[3];
                                                      strLowAirwayEndLon[tempLowAirways] = parsedLowAirways[4];
                                                      break;
                                                  case "[HIGH AIRWAY]":
                                                      tempHighAirways = tempHighAirways + 1;
                                                      string[] parsedHighAirways= input.Split(new Char[] { ' ' });
                                                      strHighAirwayID[tempHighAirways] = parsedHighAirways[0];
                                                      strHighAirwayStartLat[tempHighAirways] = parsedHighAirways[1];
                                                      strHighAirwayStartLon[tempHighAirways] = parsedHighAirways[2];
                                                      strHighAirwayEndLat[tempHighAirways] = parsedHighAirways[3];
                                                      strHighAirwayEndLon[tempHighAirways] = parsedHighAirways[4];
                                                      break;
                                                  case "[GEO]":
                                                      tempGeo = tempGeo + 1;
                                                      string[] parsedGeo = input.Split(new Char[] { ' ' });
                                                      strGeoStartLat[tempGeo] = parsedGeo[0];
                                                      strGeoStartLon[tempGeo] = parsedGeo[1];
                                                      strGeoEndLat[tempGeo] = parsedGeo[2];
                                                      strGeoEndLon[tempGeo] = parsedGeo[3];
                                                      strGeoColor[tempGeo] = parsedGeo[4];
                                                      break;
                                                  case "[ARTCC LOW]":
                                                      tempARTCCLow = tempARTCCLow + 1;
                                                      string[] parsedARTCCLow = input.Split(new Char[] { ' ' });
                                                      strArtccLowID[tempARTCCLow] = parsedARTCCLow[0];
                                                      strArtccLowStartLat[tempARTCCLow] = parsedARTCCLow[1];
                                                      strArtccLowStartLon[tempARTCCLow] = parsedARTCCLow[2];
                                                      strArtccLowEndLat[tempARTCCLow] = parsedARTCCLow[3];
                                                      strArtccLowEndLon[tempARTCCLow] = parsedARTCCLow[4];
                                                      break;
                                                  case "[ARTCC HIGH]":
                                                      tempARTCCHigh = tempARTCCHigh + 1;
                                                      string[] parsedARTCCHigh = input.Split(new Char[] { ' ' });
                                                      strArtccHighID[tempARTCCHigh] = parsedARTCCHigh[0];
                                                      strArtccHighStartLat[tempARTCCHigh] = parsedARTCCHigh[1];
                                                      strArtccHighStartLon[tempARTCCHigh] = parsedARTCCHigh[2];
                                                      strArtccHighEndLat[tempARTCCHigh] = parsedARTCCHigh[3];
                                                      strArtccHighEndLon[tempARTCCHigh] = parsedARTCCHigh[4];
                                                      break;
                                                  case "[STAR]":
                                                      tempStar = tempStar + 1;
                                                      strStarID[tempStar] = input.Substring(0, 26);
                                                      string[] parsedStars = input.Substring(26).Split(new Char[] { ' ' });
                                                      strStarStartLat[tempStar] = parsedStars[0];
                                                      strStarStartLon[tempStar] = parsedStars[1];
                                                      strStarEndLat[tempStar] = parsedStars[2];
                                                      strStarEndLon[tempStar] = parsedStars[3];
                                                      try
                                                      {
                                                          strStarColor[tempStar] = parsedStars[4];
                                                      }
                                                      catch //If No Color Provided....
                                                      {
                                                          strStarColor[tempStar] = "";
                                                      }
                                                      break;
                                                  case "[SID]":
                                                      tempSid = tempSid + 1;
                                                      strSIDID[tempSid] = input.Substring(0, 26);
                                                      string[] parsedSIDs = input.Substring(26).Split(new Char[] { ' ' });
                                                      strSIDStartLat[tempSid] = parsedSIDs[0];
                                                      strSIDStartLon[tempSid] = parsedSIDs[1];
                                                      strSIDEndLat[tempSid] = parsedSIDs[2];
                                                      strSIDEndLon[tempSid] = parsedSIDs[3];
                                                      try
                                                      {
                                                          strSIDColor[tempSid] = parsedSIDs[4];
                                                      }
                                                      catch //If No Color Provided....
                                                      {
                                                          strSIDColor[tempSid] = "";
                                                      }
                                                          break;
                                              }
                                          }
                                      }
              
                                      //Reading Done
                                      re2.Close();//Close Stream Reader
                                  }
                                  
                                  
                                
                                  
                              }
                          }
                          else
                          {
                              //File Not Found
                          }
              
                      }

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                Here's a few tips to help you compact your code down...

                1) You can use the generic List class instead of string arrays so you don't need to do the count. Define a list and add to it as such...
                Code:
                List<string> myList = new List<string>();
                myList.Add("a string!");
                Other than that, they work just like arrays.

                2) Use hash tables to eliminate your switch statement... a hash table has a key and an object. For example, you could do the following...

                Code:
                Hashtable sectionHash = new Hashtable();
                string section = ... (however you get it here);
                
                if (!sectionHash.ContainsKey(section))
                  sectionHash[section] = new List<string>();
                
                sectionHash[section].Add(... whatever ...);
                Since you've got quite a bit of stuff to add here, you could even put it all in a struct so you don't need to keep so many arrays.

                ---------------------

                Just some ideas, hopefully they're of use to you.

                Comment

                Working...