Different Ways Of Reading XML Files In ASP.NET C#

Introduction

In this article, we will see most simple and fast ways to read XML files. I hope  the code will be helpful for the developers, especially, who are going to work for the first time in XML in C#. Here, I have tried my best to put the different ways to read XML files in one place. Hope it will be helpful for the newcomers to get the overall picture and start.

Sample XML file used in this article is given below:

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Tenders>  
  3.     <Ravina>  
  4.         <BillNo>1</BillNo>  
  5.         <PageNo>10</PageNo>  
  6.         <Activity>Metals</Activity>  
  7.     </Ravina>  
  8.     <Ravina>  
  9.         <BillNo>2</BillNo>  
  10.         <PageNo>20</PageNo>  
  11.         <Activity>Formworks</Activity>  
  12.     </Ravina>  
  13.     < Ravina>  
  14.         <BillNo>3</BillNo>  
  15.         <PageNo>30</PageNo>  
  16.         <Activity>SiteWorks</Activity>  
  17.     </ Ravina>  
  18. </Tenders>  
Way 1 : Using XMLTextReader Class and Dataset

Official documentation : https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader(v=vs.110).aspx
 
Simple example is given below:

ASPX page code
  1. <asp:GridView ID="GridView1" runat="server"></asp:GridView>  
  2.   
  3. <asp:DropDownList ID="DropDownList1BillNo" runat="server"></asp:DropDownList>  
  4. <asp:DropDownList ID="DropDownList2PageNo" runat="server"></asp:DropDownList>  
  5. <asp:DropDownList ID="DropDownList3Activity" runat="server"></asp:DropDownList>  
C# code
  1. protected void Page_Load(object sender, EventArgs e)   
  2. {  
  3.         if (!IsPostBack)   
  4.         {  
  5.             BindDataToGridview();  
  6.         }  
  7.     }  
  8.     // This method is used to bind data to dropdownlist and gridview  
  9. protected void BindDataToGridview()   
  10. {  
  11.     //open the tender xml file  
  12.     XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/TernTenders.xml"));  
  13.     //reading the xml data  
  14.     DataSet ds = new DataSet();  
  15.     ds.ReadXml(xmlreader);  
  16.     xmlreader.Close();  
  17.     //if ds is not empty  
  18.     if (ds.Tables.Count != 0)   
  19.     {  
  20.         //Bind Data to gridview  
  21.         GridView1.DataSource = ds;  
  22.         GridView1.DataBind();  
  23.         // Bind Data to dropdownlist  
  24.         DropDownList1BillNo.DataSource = ds;  
  25.         DropDownList1BillNo.DataTextField = "BillNo";  
  26.         DropDownList1BillNo.DataValueField = "BillNo";  
  27.         DropDownList1BillNo.DataBind();  
  28.         DropDownList2PageNo.DataSource = ds;  
  29.         DropDownList2PageNo.DataTextField = "PageNo";  
  30.         DropDownList2PageNo.DataValueField = "PageNo";  
  31.         DropDownList2PageNo.DataBind();  
  32.         DropDownList3Activity.DataSource = ds;  
  33.         DropDownList3Activity.DataTextField = "Activity";  
  34.         DropDownList3Activity.DataValueField = "Activity";  
  35.         DropDownList3Activity.DataBind();  
  36.     }  
  37. }  
Note: In the code, mentioned above, for the dataset, we can also just pass the string filename alone, given below: 

for ex,
  1. string filePath = Server.MapPath ( "~/App_Data/TpmsReference.xml" );  
  2. ds.ReadXml ( filePath );  
Output

Output

Way 2 : Using XML Reader Class

Official documentation.

Simple Example

ASPX page code
  1. <asp:Button ID="Button1" runat="server" Text="ReadXMLDataUsingXMLReader" OnClick="Button1_Click" />    
  2. <asp:ListBox ID="ListBox1" runat="server" Height="500px" Width="500px"></asp:ListBox>  
C# code
  1. protected void Button1_Click(object sender, EventArgs e)   
  2. {  
  3.         XmlReader xReader = XmlReader.Create(Server.MapPath("~/App_Data/TernTenders.xml"));  
  4.         while (xReader.Read())   
  5.         {  
  6.             switch (xReader.NodeType)   
  7.             {  
  8.                 case XmlNodeType.Element:  
  9.                     ListBox1.Items.Add("<" + xReader.Name + ">");  
  10.                     break;  
  11.                 case XmlNodeType.Text:  
  12.                     ListBox1.Items.Add(xReader.Value);  
  13.                     break;  
  14.                 case XmlNodeType.EndElement:  
  15.                     ListBox1.Items.Add("</" + xReader.Name + ">");  
  16.                     break;  
  17.             } // end of switch  
  18.         } //end of while  
  19.     } //end of button click event  
If you want to pass the same XML data as a string, the code, given below, will work:

code

Output

Output

Way 3 : Using XMLDocument Class

Official Documentation

Simple Example

ASPX page code
  1. <asp:Button ID="ReadXmlUsingXMLDocument" runat="server" Text="ReadXMLDataUsingXMLDocument" OnClick="ReadXmlUsingXMLDocument_Click" />  
  2. <asp:ListBox ID="ListBox2" runat="server" Height="500px" Width="500px"></asp:ListBox>  
C# code
  1. protected void ReadXmlUsingXMLDocument_Click(object sender, EventArgs e)   
  2. {  
  3.     //Create the XmlDocument.  
  4.     XmlDocument doc = new XmlDocument();  
  5.     doc.Load(Server.MapPath("~/App_Data/TernTenders.xml"));  
  6.     //Display all the Activities alone in the XML  
  7.     XmlNodeList elemList = doc.GetElementsByTagName("Activity");  
  8.     for (int i = 0; i < elemList.Count; i++)   
  9.     {  
  10.         ListBox2.Items.Add(elemList[i].InnerXml);  
  11.     }  
  12. }  
Output

Output

Way 4 :Simple way of Using XMLDatasource Control

Official Documentation.

Simple example

XML data
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <tpms>  
  3.     <currencies>  
  4.         <currency Name="Indian Rupee" code="INR" />  
  5.         <currency Name="United Arab Emirates" code="AED" />  
  6.         <currency Name="Omani Rial" code="OMR" /> </currencies>  
  7. </tpms>  
ASPX code
  1. <asp:DropDownList ID="DD_CurrencyCode" runat="server" DataSourceID="tpmsreference" DataTextField="code" DataValueField="code"></asp:DropDownList>  
  2. <asp:XmlDataSource ID="tpmsreference" runat="server" DataFile="~/App_Data/TpmsReference.xml" XPath="/tpms/currencies/currency"></asp:XmlDataSource>  
Note : In the code mentioned above, two important properties should be understood clearly .This is DataFile property, which is used to load XML data from the specified XML file. XPath Property is used to apply a filter expression to the data, which had been loaded.

Output
Output

I hope the information, given above, was useful. Kindly let me know your thoughts or feedback.


Similar Articles