In this article, I am going to discuss How to Load XML Data to a Data Table using ADO.NET, and then we will see how to insert those data from the data table to a database table using C# with Examples. Please read our previous article where we discussed the Connected and Disconnected Architecture of ADO.NET with Examples.
Note: In this article, I am going to use SqlBulkCopy class to insert the data from the data table into a database table. From a performance point of view, SqlBulkCopy class makes it very easy and efficient to copy large amounts of data from a data table to a SQL Server Database Table.
The SqlBulkCopy Class in C# is used to bulk copy data from different data sources to the SQL Server database. The SqlBulkCopy class belongs to System.Data.SqlClient (this is the .NET Data Provider for SQL Server Database) namespace. This class is used to insert the data into the SQL Server Database table only i.e. the destination database must be SQL Server only. However, there is no restriction on the source data source i.e. any source data source can be used as long as the data can be loaded into the Data table.
Let us understand how to Load XML data into SQL Server Database Table using the ADO.NET SqlBulkCopy class. For this, we are going to use the following XML file. Please create a file with the name DepartmentEmployees.xml and then copy and paste the following into it.
The above XML file is going to be our Source File. As you can see, the above XML file has the Department’s and Employee’s data. Our business requirement is, we want to load the Employee’s data into the Employees database table and the Departments data into the Departments database table. And both of these tables are created in SQL Server Database. Once you load the above Employees and Departments data, the database tables should look as shown in the below image.
Let us understand How we can Load XML data into SQL Server Database Table using SqlBulkCopy class. Let us first create the SQL Server Database and the required Employees and Departments Tables by executing the following SQL Scripts.
Once you created the Database and required tables, let us see how we can load the XML data into the above database tables. So, what are going to do we will load the above XML data into the DataSet. Within the DataSet, the first DataTable is going to store the Department data and the second Data Table is going to hold the Departments from the XML file. Once we have data in the Data Tables, then we are using the SqlBulkCopy class to copy the data from the Data Tables into the corresponding Database tables. For a better understanding, please have a look at the following example which exactly does the same thing. The following example code is self-explained, so please go through the comment lines.
As you can see in the above output screenshot, the Employees and Departments Data table filling with the Data from XML File. Now, you can also verify the Employees and Departments database tables and they also hold the same data as shown in the below image.
In the next article, I am going to discuss SQL Injection and Prevention in C# with Examples. Here, in this article, I try to explain How to Load XML Data to a Data Table using ADO.NET, and then we will see how to insert those data from the data table to a database table using C# with Examples. I hope you enjoy this How to Load XML Data to a Data Table using the ADO.NET article.