XML (Extensible Markup Language) is widely used for storing and transporting data across different platforms and applications. ADO.NET provides extensive support for interacting with XML data, making it easy to load, manipulate, and update XML data in .NET applications. You can work with XML data in two primary ways: through DataSets and through direct XML processing using XML classes.
Here’s a comprehensive guide on how to interact with XML data using ADO.NET.
A DataSet in ADO.NET has built-in support for XML, making it easy to read and write data in XML format. You can load XML data directly into a DataSet and also export a DataSet to XML.
DataSet.DataSet to an XML document.DataSet can optionally include an XML schema (XSD) that defines the structure of the data.You can load an XML file or string into a DataSet using the ReadXml method.
Suppose you have an XML file employees.xml with the following structure:
<Employees>
<Employee>
<EmployeeID>1</EmployeeID>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Age>30</Age>
</Employee>
<Employee>
<EmployeeID>2</EmployeeID>
<FirstName>Jane</FirstName>
<LastName>Smith</LastName>
<Age>25</Age>
</Employee>
</Employees>
Here is an example of how to load the XML data into a DataSet:
using System;
using System.Data;
public class XmlDataSetExample
{
public void LoadXmlData()
{
// Create a new DataSet
DataSet dataSet = new DataSet();
// Load the XML data from a file
dataSet.ReadXml("employees.xml");
// Access the DataTable from the DataSet
DataTable employeesTable = dataSet.Tables["Employees"];
// Loop through the rows of the DataTable
foreach (DataRow row in employeesTable.Rows)
{
Console.WriteLine($"Employee ID: {row["EmployeeID"]}, Name: {row["FirstName"]} {row["LastName"]}, Age: {row["Age"]}");
}
}
}
ReadXml: This method loads the XML data from the specified file into the DataSet. If you have an XML string, you can load that using ReadXml with a StringReader.Tables["Employees"]: The ReadXml method automatically creates a DataTable for each root element in the XML document, based on the XML structure.DataTable in the usual way, just like with any other table in a DataSet.Once you have a DataSet, you can easily export it to an XML file or a string using the WriteXml method.
using System;
using System.Data;
public class ExportDataSetToXml
{
public void ExportData()
{
// Create a new DataSet and add a DataTable
DataSet dataSet = new DataSet();
DataTable employeesTable = new DataTable("Employees");
dataSet.Tables.Add(employeesTable);
// Add columns to the DataTable
employeesTable.Columns.Add("EmployeeID", typeof(int));
employeesTable.Columns.Add("FirstName", typeof(string));
employeesTable.Columns.Add("LastName", typeof(string));
employeesTable.Columns.Add("Age", typeof(int));
// Add some rows to the DataTable
employeesTable.Rows.Add(1, "John", "Doe", 30);
employeesTable.Rows.Add(2, "Jane", "Smith", 25);
// Export the DataSet to an XML file
dataSet.WriteXml("exported_employees.xml");
Console.WriteLine("Data has been exported to XML.");
}
}
WriteXml: This method writes the contents of the DataSet to an XML file. You can also pass a stream (like a StringWriter) to write XML to a string.DataTable inside the DataSet, formatted with the table name as the root element.In addition to the data, you can also include the XML schema (XSD) when exporting the DataSet. The schema describes the structure of the XML data.
dataSet.WriteXml("employees_with_schema.xml");
dataSet.WriteXmlSchema("employees_schema.xsd");
WriteXmlSchema: This method exports the schema (XSD) of the DataSet. The schema describes the structure of the XML document, such as the types of the columns and the relationships between tables.Once XML data is loaded into a DataSet, you can modify it just like any other data in a DataTable. You can add, update, or delete rows and columns in the DataTable.
using System;
using System.Data;
public class ModifyXmlData
{
public void ModifyData()
{
// Load the XML data into a DataSet
DataSet dataSet = new DataSet();
dataSet.ReadXml("employees.xml");
// Access the DataTable
DataTable employeesTable = dataSet.Tables["Employees"];
// Add a new row
DataRow newRow = employeesTable.NewRow();
newRow["EmployeeID"] = 3;
newRow["FirstName"] = "Michael";
newRow["LastName"] = "Johnson";
newRow["Age"] = 28;
employeesTable.Rows.Add(newRow);
// Modify an existing row
employeesTable.Rows[0]["Age"] = 31; // Change John's age to 31
// Delete a row
employeesTable.Rows[1].Delete(); // Remove the second employee
// Save the changes back to the XML file
dataSet.WriteXml("modified_employees.xml");
Console.WriteLine("XML data modified and saved.");
}
}
NewRow creates a new row, and you can set values for each column.DataRow object.Delete method of DataRow to mark a row for deletion.WriteXml to save the updated DataSet to an XML file.ADO.NET also supports LINQ queries on DataSet objects. You can use LINQ to query and filter data from the DataTable within the DataSet.
using System;
using System.Linq;
using System.Data;
public class LinqToXmlData
{
public void QueryXmlData()
{
// Load the XML data into a DataSet
DataSet dataSet = new DataSet();
dataSet.ReadXml("employees.xml");
// Access the DataTable
DataTable employeesTable = dataSet.Tables["Employees"];
// Use LINQ to query the data
var employeesOver30 = from employee in employeesTable.AsEnumerable()
where employee.Field<int>("Age") > 30
select employee;
foreach (var emp in employeesOver30)
{
Console.WriteLine($"Employee ID: {emp["EmployeeID"]}, Name: {emp["FirstName"]} {emp["LastName"]}, Age: {emp["Age"]}");
}
}
}
AsEnumerable: Converts the DataTable to an IEnumerable<DataRow>, allowing you to use LINQ methods on the data.Field<T>: A LINQ method that allows you to access data from a DataRow in a strongly-typed manner.XmlDocumentWhile the DataSet provides a powerful, table-oriented approach to XML data, you can also work with XML directly using the XmlDocument class. This class allows you to load, modify, and save XML data at a lower level, offering full flexibility for navigating and manipulating XML elements.
XmlDocument to Load and Manipulate XMLusing System;
using System.Xml;
public class XmlDocumentExample
{
public void ManipulateXml()
{
XmlDocument doc = new XmlDocument();
doc.Load("employees.xml");
// Access XML nodes
Open this section to load past papers