How to Open and read an XML file to Dataset

XML, being a platform-independent language, offers the advantage of facilitating seamless utilization of information across various platforms and operating systems. The .NET technology, renowned for its comprehensive support, readily accommodates the XML file format. The .NET Framework provides a rich set of classes specifically designed for reading, writing, and performing various operations on XML-formatted files. These classes equip developers with the necessary tools and functionalities to effectively interact with XML files, ensuring smooth integration and efficient data handling within the .NET ecosystem.

XmlReader

In the previous section, we explored the process of reading an XML file by navigating through nodes. Now, we will investigate into an alternative approach by utilizing a DataSet to read an XML file. In this method, the DataSet employs an XmlReader to access and retrieve the contents of the XML file. By utilizing the XmlReader, we can locate the XML file and pass it as an argument to the DataSet, allowing for efficient extraction and processing of the XML data. This approach provides an alternative pathway for reading XML files, expanding our options for effectively working with XML data within the DataSet framework.

Click here to download the input file product.xml : product.xml

Full Source C#
using System; using System.Data; using System.Windows.Forms; using System.Xml; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XmlReader xmlFile ; xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings()); DataSet ds = new DataSet(); ds.ReadXml(xmlFile); int i = 0; for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[2].ToString()); } } } }