The goal is to populate a dataset from XML, sort one of the DataTables, and then write out the sorted DataSet back to XML.
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.
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);
UPDATE
Now it removes the other tables from the dataset. This is how the xml should look:
<user_data>
<settings>
<a></a>
<b></b>
<c></c>
</settings>
<perms>
<a></a>
<b></b>
<c></c>
</perms>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</user_data>
And it looks like this, removing the other tables but the sort works fine :)
</DocumentElement>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
<cj>
<a></a>
<b></b>
<c></c>
</cj>
</DocumentElement>
How can I use C# to sort just DataTable cj
, and then write the entire DataSet back to XML? The settings
and perms
should remain untouched.