This utility makes it easy to create serialized XML DataSet's based on SQL queries run against a SQL Server.
- Run this program to execute a SQL statement
- Copy the resulting XML into a file in your .NET unit test project
- Embed the XML file you created
- In your test code, load the embedded XML, deserialize it back into a DataSet
You now have a way to mock a DataSet that would normally be loaded from a database.
How to deserialize the DataTable:
/// <summary>
/// Reads a <see cref="DataTable"/> from an embedded XML
/// document that contains the serialized version.
/// </summary>
/// <param name="sourceAssembly">
/// The assembly that contains the embedded file containing
/// the XML.
/// </param>
/// <param name="resourceName">
/// The qualified name of the resource to load.
/// </param>
/// <returns>
/// The reconstituted <see cref="DataTable"/>.
/// </returns>
public static DataTable ReadEmbeddedDataTable(Assembly sourceAssembly, string resourceName)
{
using (Stream readStream = sourceAssembly.GetManifestResourceStream(resourceName))
{
XmlSerializer xs = new XmlSerializer(typeof(DataSet));
DataSet ds = (DataSet)xs.Deserialize(readStream);
return ds.Tables[0];
}
}Related Blog Posts: