Here's a little sample. You'll notice that I switch the
DataColumn.ColumnMapping so show writing out column as attributes and
also as elements (by default you don't get the schema written out)
using(DataSet myDataSet = new DataSet())
{
using(DataTable myDataTable = new DataTable("MyData"))
{
myDataSet.Tables.Add(myDataTable);
myDataTable.Columns.Add(new DataColumn("EmployeeID", typeof(Int32)));
myDataTable.Columns.Add(new DataColumn("EmployeeName",
typeof(string)));
DataRowCollection rows = myDataTable.Rows;
DataRow newRow = myDataTable.NewRow();
newRow["EmployeeID"] = 1;
newRow["EmployeeName"] = "Jason Hales";
rows.Add(newRow);
newRow = myDataTable.NewRow();
newRow["EmployeeID"] = 2;
newRow["EmployeeName"] = "Bob Smith";
rows.Add(newRow);
foreach(DataColumn column in myDataTable.Columns)
{
column.ColumnMapping = MappingType.Attribute;
}
myDataSet.WriteXml(@"c:\temp\data with attributes.xml");
foreach(DataColumn column in myDataTable.Columns)
{
column.ColumnMapping = MappingType.Element;
}
myDataSet.WriteXml(@"c:\temp\data with elements.xml");
}
}
Results in :
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<MyData EmployeeID="1" EmployeeName="Jason Hales" />
<MyData EmployeeID="2" EmployeeName="Bob Smith" />
</NewDataSet>
and:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<MyData>
<EmployeeID>1</EmployeeID>
<EmployeeName>Jason Hales</EmployeeName>
</MyData>
<MyData>
<EmployeeID>2</EmployeeID>
<EmployeeName>Bob Smith</EmployeeName>
</MyData>
</NewDataSet>