MSDN Logo

MSDN Documentation

XML Support in ADO.NET

This document provides an in-depth overview of how ADO.NET integrates with XML, enabling developers to efficiently work with XML data within their .NET applications. ADO.NET offers powerful features for reading, writing, and transforming XML data, and for seamlessly converting between XML and ADO.NET DataSet objects.

Key Concepts and Features

Loading XML into a DataSet

The DataSet object provides built-in support for loading XML data directly from a file or stream. This is particularly useful when dealing with structured data that is already in XML format.


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

// Assuming xmlFilePath points to your XML file
string xmlFilePath = "data.xml";
DataSet ds = new DataSet();

try {
    ds.ReadXml(xmlFilePath);
    Console.WriteLine("XML loaded into DataSet successfully.");
} catch (Exception ex) {
    Console.WriteLine($"Error loading XML: {ex.Message}");
}
            

Writing a DataSet to XML

You can also easily persist the contents of a DataSet to an XML file or stream. This is valuable for saving application state, transferring data, or generating reports.


// Assuming ds is a populated DataSet
string outputXmlPath = "output.xml";

try {
    ds.WriteXml(outputXmlPath);
    Console.WriteLine($"DataSet written to XML: {outputXmlPath}");
} catch (Exception ex) {
    Console.WriteLine($"Error writing XML: {ex.Message}");
}
            

In-Memory XML Handling with XmlDataDocument

For scenarios requiring more direct manipulation of XML data in memory while maintaining a connection to relational data, ADO.NET provides the XmlDataDocument class. This class allows you to treat an XML document as a DataSet, enabling you to navigate and query XML nodes using familiar data access methods.


using System.Xml;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("my_document.xml"); // Load your XML

XmlDataDocument xmlDataDoc = new XmlDataDocument(new DataSet());
xmlDataDoc.LoadXml(xmlDoc.OuterXml);

// You can now query xmlDataDoc like a DataSet
XmlNodeList nodeList = xmlDataDoc.SelectNodes("//item");
foreach (XmlNode node in nodeList) {
    Console.WriteLine(node.Attributes["name"].Value);
}
            

DataSet XML Schema Support

When a DataSet is written to XML, it includes schema information by default, allowing it to be read back accurately. You can control this behavior, including writing only the data or only the schema.

Transforming XML with XSLT

ADO.NET seamlessly integrates with .NET's XSLT capabilities, allowing you to transform XML data loaded into a DataSet or directly from XML files into various formats, such as HTML or other XML structures.


using System.Xml.Xsl;

// Load DataSet and XSLT stylesheet
DataSet dsForTransform = new DataSet();
dsForTransform.ReadXml("data.xml");
XslTransform xslt = new XslTransform();
xslt.Load("transform.xslt");

// Create an XML writer for the output
using (StringWriter sw = new StringWriter()) {
    xslt.Transform(dsForTransform, null, sw);
    string htmlResult = sw.ToString();
    Console.WriteLine("Transformed HTML:\n" + htmlResult);
}
            
Tip: For optimal performance when dealing with large XML files, consider using techniques like streaming with XmlReader and XmlWriter instead of loading the entire document into memory.

Common Scenarios