Reading data from XML File
In my last articles, I have discussed about Reading and Writing data from Excel file to GridView and Saving to SQL Table.
This article is about Reading data from XML File. We will use a XML File as our datasource on different ways.
In my last articles, I have discussed about Reading and Writing data from Excel file to GridView and Saving to SQL Table.
Today, I am going to Read data from XML File. XML means Extensible Markup Language. As you know, the complete .NET is based on XML and XML is the most popular and platform independent Schema. Which powers all the Documents, Spreadsheets and other common file formats.
So, lets Read XML File and bind its data in GridView.
We will do this in multiple ways. Basically, Supplying file from Front-End and saved file in server and coded at the time of programming.
To do this, XML File should be declared in structured Node. Limitations:
One XML File can contain Only one Root Node.
Multiple Tables can be created inside one Node.
Sample Design of XML File with following Table Structure.
Roll Name Maths English Science Social
XML Design with Table Name classXII inside Result node.
<result>
<classXII>
<Roll>1</Roll>
<Name>Abhaya</Name>
<Maths>67</Maths>
<English>75</English>
<Science>80</Science>
<Social>62</Social>
</classXII>
<classXII>
<Roll>2</Roll>
<Name>Vikas</Name>
<Maths>44</Maths>
<English>71</English>
<Science>60</Science>
<Social>56</Social>
</classXII>
<classXII>
<Roll>3</Roll>
<Name>Rakesh</Name>
<Maths>92</Maths>
<English>87</English>
<Science>90</Science>
<Social>60</Social>
</classXII>
<classXII>
<Roll>4</Roll>
<Name>Deepak</Name>
<Maths>88</Maths>
<English>45</English>
<Science>70</Science>
<Social>70</Social>
</classXII>
</result>
You can include multiple tables like below designs.
<result>
<classXII>
<Roll>1</Roll>
<Name>Abhaya</Name>
<Maths>67</Maths>
<English>75</English>
<Science>80</Science>
<Social>62</Social>
</classXII>
<classXI>
<Roll>101</Roll>
<Name>Abhaya Kumar</Name>
<Maths>67</Maths>
<English>75</English>
<Science>80</Science>
<Social>62</Social>
</classXI>
</result>
Design of the ASPX Page.
Code
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Reading date from XML File</title>
</head>
<body>
<form id="form1" runat="server">
<h2 style="text-align: center">Reading Data from XML</h2>
<br />
<center>
XML File :
<asp:FileUpload ID="FileUpload1" runat="server" /> <br />
<br />
Table Name :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Style="text-align: center" Width="451px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</center>
<br />
<br />
</form>
</body>
</html>
Code to Bind data at BackEnd.
Namespace Required : System.Data
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("Data.xml"));
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
I have included an Attachment of Project, which can be downloaded and contains XML Data file, Backend codes for multiple methods, Uploading and Reading file and much more.
Feedback and suggestions are always helpful.
Glad to Be,
John Bhatt
P.Yar.B Complex