title: sorting a DataSet between reading and writing to XML
body: <p>The goal is to populate a dataset from XML, sort one of the DataTables, and then write out the sorted DataSet back to XML.</p>
  
  <p>I can't seem to sort my DataSet and have tried a few variations. It adds the data fine but does not sort. Hope someone could help to stop me pulling my hair out.</p>
  
  <pre><code>DataSet dsXml = new DataSet();
  dsXml.ReadXml(msXml);
  DataTable tbl;
  // Read DataTable.
  tbl = dsXml.Tables["cj"];
  // Set Primary Key
  tbl.PrimaryKey = new DataColumn[] { tbl.Columns["a"] };
  DataView dtView = tbl.DefaultView;
  DataRow foundRow = tbl.Rows.Find(findCookiejarId);
  tbl.Rows.Add(addRow);
  dtView.ToTable().Sort = "a ASC";
  StringWriter sw = new StringWriter();
  dsXml.WriteXml(sw);
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Now it removes the other tables from the dataset. This is how the xml should look:</p>
  
  <pre><code>&lt;user_data&gt;
    &lt;settings&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/settings&gt;
    &lt;perms&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/perms&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
  &lt;/user_data&gt;
  </code></pre>
  
  <p>And it looks like this, removing the other tables but the sort works fine :)</p>
  
  <pre><code>&lt;/DocumentElement&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
    &lt;cj&gt;
      &lt;a&gt;&lt;/a&gt;
      &lt;b&gt;&lt;/b&gt;
      &lt;c&gt;&lt;/c&gt;
    &lt;/cj&gt;
  &lt;/DocumentElement&gt;
  </code></pre>
  
  <p>How can I use C# to sort just DataTable <code>cj</code>, and then write the entire DataSet back to XML? The <code>settings</code> and <code>perms</code> should remain untouched.</p>