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>
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
- <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>
C# code
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindDataToGridview();
- }
- }
-
- protected void BindDataToGridview()
- {
-
- XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/TernTenders.xml"));
-
- DataSet ds = new DataSet();
- ds.ReadXml(xmlreader);
- xmlreader.Close();
-
- if (ds.Tables.Count != 0)
- {
-
- GridView1.DataSource = ds;
- GridView1.DataBind();
-
- 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();
- }
- }
Note: In the code, mentioned above, for the dataset, we can also just pass the string filename alone, given below:
for ex,
- string filePath = Server.MapPath ( "~/App_Data/TpmsReference.xml" );
- ds.ReadXml ( filePath );
Output
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>
C# code
- 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;
- }
- }
- }
If you want to pass the same XML data as a string, the code, given below, will work:
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>
C# code
- protected void ReadXmlUsingXMLDocument_Click(object sender, EventArgs e)
- {
-
- XmlDocument doc = new XmlDocument();
- doc.Load(Server.MapPath("~/App_Data/TernTenders.xml"));
-
- XmlNodeList elemList = doc.GetElementsByTagName("Activity");
- for (int i = 0; i < elemList.Count; i++)
- {
- ListBox2.Items.Add(elemList[i].InnerXml);
- }
- }
Output
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>
ASPX code
- <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>
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
I hope the information, given above, was useful. Kindly let me know your thoughts or feedback.