title: Custom File Parser
body: <p>I am building a parser for a custom pipe delimited file format and I am finding my code to be very bulky, could someone suggest better methods of parsing this data? </p>
  
  <p>The file's data is broken down by a line delimited by a pipe (|), each line starts with a record type, followed by an ID, followed by different number of columns after. </p>
  
  <p><strong>Ex:</strong> 
  CDI|11111|OTHERDATA|somemore|other</p>
  
  <p>CEX001|123131|DATA|data</p>
  
  <p>CCC|123131|DATA|data1|data2|data3|data4|data5|data6</p>
  
  <p>. I am splitting by pipe, then grabbing the first two columns, and then using a switch checking the first line and calling a function that will parse the remaining into an object purpose built for that record type. I would really like a more elegant method. </p>
  
  <pre><code>    public Dictionary&lt;string, DataRecord&gt; Parse()
      { 
          var data = new Dictionary&lt;string, DataRecord&gt;();
  
          var rawDataDict = new Dictionary&lt;string, List&lt;List&lt;string&gt;&gt;&gt;();
          foreach (var line in File.ReadLines(_path))
          {
              var split = line.Split('|');
              var Id = split[1];
              if (!rawDataDict.ContainsKey(Id))
              {
                  rawDataDict.Add(Id, new List&lt;List&lt;string&gt;&gt; {split.ToList()});
              }
              else
              {
                  rawDataDict[Id].Add(split.ToList());
              }
          }
  
          rawDataDict.ToList().ForEach(pair =&gt;
          {
              var key = pair.Key.ToString();
              var values = pair.Value;
  
              foreach (var value in values)
              {
  
                  var recordType = value[0];
  
                  switch (recordType)
                  {
                      case "CDI":
                          var cdiRecord = ParseCdi(value);
                          if (!data.ContainsKey(key))
                          {
                              data.Add(key, new DataRecord
                              {
                                  Id = key, CdiRecords = new List&lt;CdiRecord&gt;() {  cdiRecord }
                              });
                          }
                          else
                          {
                              data[key].CdiRecords.Add(cdiRecord);
                          }
                          break;
                      case "CEX015":
                          var cexRecord = ParseCex(value);
                          if (!data.ContainsKey(key))
                          {
                              data.Add(key, new DataRecord
                              {
                                  Id = key,
                                  CexRecords = new List&lt;Cex015Record&gt;() { cexRecord }
                              });
                          }
                          else
                          {
                              data[key].CexRecords.Add(cexRecord);
                          }
                          break;
                      case "CPH":
                          CphRecord cphRecord = ParseCph(value);
                          if (!data.ContainsKey(key))
                          {
                              data.Add(key, new DataRecord
                              {
                                  Id = key,
                                  CphRecords = new List&lt;CphRecord&gt;() { cphRecord }
                              });
                          }
                          else
                          {
                              data[key].CphRecords.Add(cphRecord);
                          }
                          break;
                  }
              }
          });
  
          return data;
      }
  </code></pre>