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:
- <?xml version="1.0" encoding="utf-8" ?>
- <Tenders>
- <Ravina>
- <BillNo>1</BillNo>
- <PageNo>10</PageNo>
- <Activity>Metals</Activity>
- </Ravina>
- <Ravina>
- <BillNo>2</BillNo>
- <PageNo>20</PageNo>
- <Activity>Formworks</Activity>
- </Ravina>
- < Ravina>
- <BillNo>3</BillNo>
- <PageNo>30</PageNo>
- <Activity>SiteWorks</Activity>
- </ Ravina>
- </Tenders>
Official documentation : https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader(v=vs.110).aspx
ASPX page code
- <asp:GridView ID="GridView1" runat="server"></asp:GridView>
- <asp:DropDownList ID="DropDownList1BillNo" runat="server"></asp:DropDownList>
- <asp:DropDownList ID="DropDownList2PageNo" runat="server"></asp:DropDownList>
- <asp:DropDownList ID="DropDownList3Activity" runat="server"></asp:DropDownList>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindDataToGridview();
- }
- }
- // This method is used to bind data to dropdownlist and gridview
- protected void BindDataToGridview()
- {
- //open the tender xml file
- XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/TernTenders.xml"));
- //reading the xml data
- DataSet ds = new DataSet();
- ds.ReadXml(xmlreader);
- xmlreader.Close();
- //if ds is not empty
- if (ds.Tables.Count != 0)
- {
- //Bind Data to gridview
- GridView1.DataSource = ds;
- GridView1.DataBind();
- // Bind Data to dropdownlist
- DropDownList1BillNo.DataSource = ds;
- DropDownList1BillNo.DataTextField = "BillNo";
- DropDownList1BillNo.DataValueField = "BillNo";
- DropDownList1BillNo.DataBind();
- DropDownList2PageNo.DataSource = ds;
- DropDownList2PageNo.DataTextField = "PageNo";
- DropDownList2PageNo.DataValueField = "PageNo";
- DropDownList2PageNo.DataBind();
- DropDownList3Activity.DataSource = ds;
- DropDownList3Activity.DataTextField = "Activity";
- DropDownList3Activity.DataValueField = "Activity";
- DropDownList3Activity.DataBind();
- }
- }
for ex,
- string filePath = Server.MapPath ( "~/App_Data/TpmsReference.xml" );
- ds.ReadXml ( filePath );

Way 2 : Using XML Reader Class
Official documentation.
Simple Example
ASPX page code
- <asp:Button ID="Button1" runat="server" Text="ReadXMLDataUsingXMLReader" OnClick="Button1_Click" />
- <asp:ListBox ID="ListBox1" runat="server" Height="500px" Width="500px"></asp:ListBox>
- protected void Button1_Click(object sender, EventArgs e)
- {
- XmlReader xReader = XmlReader.Create(Server.MapPath("~/App_Data/TernTenders.xml"));
- while (xReader.Read())
- {
- switch (xReader.NodeType)
- {
- case XmlNodeType.Element:
- ListBox1.Items.Add("<" + xReader.Name + ">");
- break;
- case XmlNodeType.Text:
- ListBox1.Items.Add(xReader.Value);
- break;
- case XmlNodeType.EndElement:
- ListBox1.Items.Add("</" + xReader.Name + ">");
- break;
- } // end of switch
- } //end of while
- } //end of button click event

Output

Way 3 : Using XMLDocument Class
Official Documentation
Simple Example
ASPX page code
- <asp:Button ID="ReadXmlUsingXMLDocument" runat="server" Text="ReadXMLDataUsingXMLDocument" OnClick="ReadXmlUsingXMLDocument_Click" />
- <asp:ListBox ID="ListBox2" runat="server" Height="500px" Width="500px"></asp:ListBox>
- protected void ReadXmlUsingXMLDocument_Click(object sender, EventArgs e)
- {
- //Create the XmlDocument.
- XmlDocument doc = new XmlDocument();
- doc.Load(Server.MapPath("~/App_Data/TernTenders.xml"));
- //Display all the Activities alone in the XML
- XmlNodeList elemList = doc.GetElementsByTagName("Activity");
- for (int i = 0; i < elemList.Count; i++)
- {
- ListBox2.Items.Add(elemList[i].InnerXml);
- }
- }

Way 4 :Simple way of Using XMLDatasource Control
Official Documentation.
Simple example
XML data
- <?xml version="1.0" encoding="utf-8" ?>
- <tpms>
- <currencies>
- <currency Name="Indian Rupee" code="INR" />
- <currency Name="United Arab Emirates" code="AED" />
- <currency Name="Omani Rial" code="OMR" /> </currencies>
- </tpms>
- <asp:DropDownList ID="DD_CurrencyCode" runat="server" DataSourceID="tpmsreference" DataTextField="code" DataValueField="code"></asp:DropDownList>
- <asp:XmlDataSource ID="tpmsreference" runat="server" DataFile="~/App_Data/TpmsReference.xml" XPath="/tpms/currencies/currency"></asp:XmlDataSource>
Output

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


Partha MaityPosted Jun 17, 2018, 3:12 AM EDT
Thanks a lot, it's working for me
Karthik ElumalaiPosted Aug 15, 2016, 5:51 PM EDT
My PleasureRavi Kandel.Thank you very much for you valuable feedback.
Ravi KandelPosted Aug 13, 2016, 10:59 PM EDT
Thanks
Karthik ElumalaiPosted Aug 11, 2016, 7:38 AM EDT
Thank you very much Vignesh Mani
Vignesh ManiTOP 100Posted Aug 11, 2016, 7:37 AM EDT
NIce
Karthik ElumalaiPosted Aug 11, 2016, 12:54 AM EDT
ThanksManas Mohapatra.
Manas MohapatraPosted Aug 10, 2016, 8:12 PM EDT
Good to know multiple ways to read xml data from files..