title: Turn a System.Collections.Generic.Dictionary into an XDocument
body: <p>I am trying to make a Dictionary into an XDocument (XML) for easier parsing later in my application.</p>
  
  <p>The application will generate a Word Document based on the XDocument. My mate will send me a Dictionary so I have to convert it myself.</p>
  
  <p>This is what I got so far, but I am stuck. I can't really think of how to continue:</p>
  
  <pre><code>static XDocument Parse(Dictionary&lt;String, String&gt; dic)
      {
          XDocument newXDoc = new XDocument();
          List&lt;String&gt; paragraphs = new List&lt;String&gt;();
          int count = 0;
          foreach(KeyValuePair&lt;string,string&gt; pair in dic)
          {
              if (pair.Key.Equals("paragraph" + count))
              {
                  paragraphs.Add(pair.Value);
                  count++;
              }
          }
          newXDoc.Add(new XElement("document",
              new XElement("title",dic["title"])));
          return newXDoc;
      }
  </code></pre>
  
  <p>So to explain:</p>
  
  <p>My idea is to make the XML document like this:</p>
  
  <pre><code>&lt;document&gt;
      &lt;title&gt;Some Title&lt;/title&gt;
      &lt;paragraph0&gt;some text&lt;/paragraph0&gt;
      &lt;paragraph1&gt;some more text on a new line&lt;/paragraph1&gt;
      &lt;paragraph2&gt;
          &lt;list&gt;
              &lt;point&gt;Some Point&lt;/point&gt;
              &lt;point&gt;Some other point&lt;/point&gt;
          &lt;/list&gt;
      &lt;/paragraph2&gt;
      &lt;header&gt;
          &lt;author&gt;me&lt;/author&gt;
          &lt;date&gt;today&lt;/date&gt;
          &lt;subject&gt;some subject&lt;/subject&gt;
      &lt;/header&gt;
  &lt;/document&gt;
  </code></pre>
  
  <p>My problem is that I never know how many paragraphs I will receive given that what I am sent is just a dictionary. As you can probably tell, I was pondering over how to make a nice <code>foreach</code>construction which could:</p>
  
  <ol>
  <li>Take out all the Paragraphs temporarily</li>
  <li>Fill in the XDocument with appropriate XElement's</li>
  </ol>
  
  <p>I just don't know how to do this without running into possible NullPointerExceptions or the likes. Any help with this?</p>
  
  <p>Short version: How do I parse a Dictionary to an XDocument given the above structure, not knowing how many paragraphs I can possibly get?</p>
  
  <p><strong>A new paragraph is defined as when the previous paragraph reaches a newline character (\n)</strong></p>