ADO.NET XML Integration

ADO.NET provides robust support for working with XML data, enabling seamless integration between relational data and XML formats. This integration allows developers to easily transform, manipulate, and persist data in XML structures.

Key Features for XML Integration

Loading and Saving Data to XML

The DataSet class has methods to directly load XML data into a dataset and to write the dataset's contents to an XML file.

When saving, ADO.NET can preserve schema information, allowing for round-trip data loading.

Tip: Use DataSet.WriteXmlSchema(string path) to save the schema of a DataSet separately.

Transforming Relational Data to XML

ADO.NET leverages the power of XSLT (Extensible Stylesheet Language Transformations) to transform the XML representation of a DataSet into different formats. This is particularly useful for generating reports, custom data feeds, or integrating with other systems.

You can use the XmlDataDocument class in conjunction with XSLT stylesheets to perform these transformations.

Working with XML Schemas

ADO.NET can read and write XML schemas (XSD files). This allows for validation of XML data against a defined structure and for the creation of DataSet objects that adhere to a specific schema.

LINQ to XML

While ADO.NET's primary XML integration is through DataSet, modern .NET development often utilizes LINQ to XML for more direct and flexible XML manipulation. Developers can combine the strengths of both technologies for complex data scenarios.

Code Example: Reading and Writing XML with DataSet


using System;
using System.Data;
using System.IO;

public class AdoNetXmlExample
{
    public static void Main(string[] args)
    {
        // Create a new DataSet
        DataSet ds = new DataSet("MyDataSet");

        // Create a DataTable
        DataTable dt = new DataTable("Products");
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Price", typeof(decimal));

        // Add some data
        dt.Rows.Add(1, "Laptop", 1200.50m);
        dt.Rows.Add(2, "Mouse", 25.99m);
        dt.Rows.Add(3, "Keyboard", 75.00m);

        ds.Tables.Add(dt);

        // Define file paths
        string xmlFilePath = "products.xml";
        string schemaFilePath = "products.xsd";

        try
        {
            // Write DataSet to XML
            ds.WriteXml(xmlFilePath);
            Console.WriteLine($"DataSet written to XML: {Path.GetFullPath(xmlFilePath)}");

            // Write DataSet schema to XSD
            ds.WriteXmlSchema(schemaFilePath);
            Console.WriteLine($"DataSet schema written to XSD: {Path.GetFullPath(schemaFilePath)}");

            // Clear the DataSet
            ds.Clear();

            // Read XML back into DataSet
            ds.ReadXml(xmlFilePath);
            Console.WriteLine($"XML data read back into DataSet.");

            // Display data from the reloaded DataSet
            Console.WriteLine("\nData from reloaded DataSet:");
            foreach (DataRow row in ds.Tables["Products"].Rows)
            {
                Console.WriteLine($"ID: {row["ID"]}, Name: {row["Name"]}, Price: {row["Price"]}");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
            
Note: Ensure you have the necessary file permissions to create and write to the specified file paths.

Performance Considerations

While DataSet is powerful, for very large XML documents or high-performance scenarios, consider using streaming APIs like XmlReader and XmlWriter, or LINQ to XML.

Further Reading