This seems like it should be so trivial, but try as I might I can't figure it out.
I want to write out a dataset as xml (i.e., DataSet.WriteXml()) but use the tablename as the root element instead of the datasetname. For example, if I have:
private void simpleDataSetXml()
{
DataSet ds = new DataSet();
DataTable table = new DataTable("foo");
DataColumn col = new DataColumn("bar", System.Type.GetType("System.String"));
table.Columns.Add(col);
DataRow row = table.NewRow();
row["bar"] = "baz";
table.Rows.Add(row);
ds.Tables.Add(table);
StringWriter writer = new StringWriter();
ds.WriteXml(writer);
System.Console.WriteLine(writer.ToString());
}
What I'd like to see is:
<foo>
<bar>baz</bar>
</foo>
Instead of:
<NewDataSet>
<foo>
<bar>baz</bar>
</foo>
</NewDataSet>
I know that it's somehow possible because if I do a DataSet.ReadXml() of the following:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>baz</bar>
</foo>
And then do a DataSet.WriteXml() then it gives me exactly what I want. When I compare the dataset structures in the debugger the only difference I see between my programmatically-generated dataset and the one created from the XML is that the dataset created from XML has:
fTopLevelTable = true
Whereas the programmatically generated one has fTopLevelTable = false.
Now fTopLevelTable is a private variable so I can't just modify it, but I can't for the life of me figure out what property to set or method to call to get it to be true.
Any help would be greatly appreciated.
-Dave