C# code to convert XML into Dataset

How to convert XML file into dataset in C# programming?
In this blog I will show you how we can get XML data into a dataset.

What is XML?

XML is a markup language developed by the World Wide Web Consortium (W3C). The full form of XML is Extensible Markup Language.


The way to write code in XML is very similar to HTML. HTML and XML both languages use tags. Using different tags in HTML, it is described how the contents of the page (eg text, images, etc.) will be displayed on the user’s screen, while the data is stored and managed using tags in XML.

Below is the XML file structure which tells about product category and item details.

XML Structure

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <product category="electronics">
    <item name="tv" price="12000"></item>
    <item name="mobile" price="7000"></item>
    <item name="ac" price="25000"></item>
    <item name="laptop" price="22000"></item>
  </product>
  <product category="furniture">
    <item name="chair" price="1200"></item>
    <item name="table" price="2000"></item>
    <item name="computer table" price="2500"></item>   
  </product>
</root>

C# Code

DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/File1.xml"));


In Data Visualizer we may found 2 tables i.e. product and item. Refer below screen shot.

Leave a Comment

RSS
YouTube
YouTube
Instagram