title: C# split string to array then to key value pair in dictionary
body: <p>I'm currently passing a string, with multiple delimiters, to a method that I would like to initially split (at ~) into a string array, then each entry in the array (at |) to a dictionary ("key"="value").</p>
  
  <pre><code>~timestamp=Mar  1 2018  3:14PM|proc=procedure A|tran=transaction 1|rowCount=987
  ~timestamp=Mar  1 2018  3:14PM|proc=procedure B|tran=transaction 2|rowCount=654
  ~timestamp=Mar  1 2018  3:14PM|proc=procedure C|tran=transaction 3|rowCount=321
  </code></pre>
  
  <p>I can accomplish the initial split into individual "rows"</p>
  
  <pre><code>~timestamp=Mar 1 2018 3:14PM|proc=procedure A|tran=transaction 1|rowCount=987
  </code></pre>
  
  <p>with:</p>
  
  <pre><code>string result = &lt;&lt;long string&gt;&gt;;
  string filepath = &lt;&lt;filepath variable&gt;&gt;;
  string[] resultRows = result.Split('~');
  
  try
  {
      foreach (string row in resultRows)
      {
          if (!File.Exists(filepath))
          {
              using (StreamWriter log = File.CreateText(filepath))
              {
                  log.WriteLine(row);
              }
          }
          else
          {
              using (StreamWriter log = File.AppendText(filepath))
              {
                  log.WriteLine(row);
              }
          }
      }
  }
  catch (Exception e)
  {
      Console.WriteLine(e.Message);
  }
  </code></pre>
  
  <p>However, when I try to then parse each of those "rows" to a Dictionary, an error (Index was outside the bounds of the array) is thrown.</p>
  
  <pre><code>string result = &lt;&lt;long string&gt;&gt;;
  string filepath = &lt;&lt;filepath variable&gt;&gt;;
  string[] resultRows = result.Split('~');
  Dictionary&lt;string, string&gt; dict = new Dictionary&lt;string, string&gt;();
  
  try
  {
      foreach (string row in resultRows)
      {
          var results = row.Split('|').Select(s =&gt; s.Split(new[] { '=' }));
  
          foreach (var item in results)
          {
              dict.Add(item[0], item[1]);
          }
      }
  }
  catch (Exception e)
  {
      Console.WriteLine(e.Message);
  }
  </code></pre>
  
  <p>I'm assuming that my expectations of the dictionary aren't correct -- I would like to see something along the lines of the following, for each "entry" in the Dictionary:</p>
  
  <pre><code>~timestamp=&gt;Mar 1 2018 3:14PM, proc=&gt;procedureA, tran=&gt;transaction1, rowCount=&gt;987
  </code></pre>
  
  <p>so that I can then write each line to a log file, where I can access the value for each key individually.  If my understanding of how to set/get values in a dictionary are way off, is there a better way to accomplish this task?</p>