XML
XML and the DataSet
At the heart of the .NET class library is XML, and this is true with the DataSet as well. Here are some of the XML features
of the DataSet:
WriteXml
The WriteXml method writes the data, and optionally the schema, in a DataSet. There are several
overloads of this method, allowing you to write the XML to any Stream, file, TextWrite, or XmlWriter, as
well as overloads allowing you to write the schema as well.
Here are the different overloads of the WriteXml method:
public void WriteXml(Stream);
public void WriteXml(Stream, XmlWriteMode);
public void WriteXml(string);
public void WriteXml(string, XmlWriteMode);
public void WriteXml(TextWriter);
public void WriteXml(TextWriter, XmlWriteMode);
public void WriteXml(XmlWriter);
public void WriteXml(XmlWriter, XmlWriteMode);
The values for the XmlWriteMode enumeration are:
| Value |
Description |
| DiffGram |
Writes the DataSet as a DiffGram, including changed and original values. |
| IgnoreSchema |
Writes the XML without any schema |
| WriteSchema |
Default. Writes the XML data with an inline schema. |
When you write the XML, by default it will have the following format:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table>
<customerID>8</customerID>
<customerName>Wily Widgets</customerName>
</Table>
</NewDataSet>
The root node of the XML document is the name of the DataSet. This can be indicated at time of instantiation, or later with the DataSetName property, as in the following lines of code:
DataSet objDataSet = new DataSet("CustomersDataSet");
objDataSet.DataSetName = "CustomersDataSet";
The rows of data are enclosed in a tag with the name of the table, set when you use a DataAdapter to fill the DataSet, or by setting the TableName property of the Table in the DataSet. The following code snippet
shows these two cases:
SqlDataAdapter da = new SqlDataAdapter(objCmd);
da.Fill(objDataSet, "CustomersTable");
objDataSet.Tables[0].TableName = "CustomersTable";
Using the WriteXml method after setting these properties results in the XML file below. Notice our DataSet name, CustomersDataSet, and our Table name, CustomersTable, in the output:
<?xml version="1.0" standalone="yes"?>
<CustomersDataSet>
<CustomersTable>
<customerID>8</customerID>
<customerName>Wily Widgets</customerName>
</CustomersTable>
</CustomersDataSet>
If you would prefer to have the columns mapped as attributes of the XML element, rather than individual elements, you have to change the ColumnMapping property of each column in your DataSet Table to MappingType.Attribute, as in the following example:
foreach(DataColumn c in objDataSet.Tables[0].Columns)
{
 c.ColumnMapping = MappingType.Attribute;
}
Here is the output using the same Customers data as in the previous examples:
<?xml version="1.0" standalone="yes"?>
<CustomersDataSet>
<CustomersTable customerID="8" customerName="Wily Widgets" />
</CustomersDataSet>
ReadXml
In addition to being able to extract XML from a DataSet, you can also load XML data into a DataSet using the ReadXml method.
Here are the various overloads of the ReadXml method:
public void ReadXml(Stream);
public void ReadXml(Stream, XmlReadMode);
public void ReadXml(string);
public void ReadXml(string, XmlReadMode);
public void ReadXml(TextReader);
public void ReadXml(TextReader, XmlReadMode);
public void ReadXml(XmlReader);
public void ReadXml(XmlReader, XmlReadMode);
The values for the XmlReadMode enumeration are:
| Value |
Description |
| Auto |
The default. Will use DiffGram, ReadSchema, or InferSchema mode as appropriate. |
| DiffGram |
Reads a diffgram and applies the changes to the dataset. |
| Fragment |
Reads xml documents, such as those generated by FOR XML sql queries. |
| IgnoreSchema |
Ignores any schema in the XML file, and loads data using the current schema in the DataSet. Used when the DataSet is already configured with a schema. |
| InferSchema |
Determines the schema based on the format of the data, and loads the data. Used when neither the DataSet nor the XML file has a schema. |
| ReadSchema |
Reads the schema in the XML file to configure the DataSet, and loads the data. |