Pages

Friday 14 September 2012

How to Read Data from XML File in ASP.Net(Example-1) ?

Suppose we have an XML file "Customer.xml", whose values we want to read.

Step-1: Add a page to your solution and place a button in it.
<asp:Button ID="btnReadXML" runat="server" Text="Read XML"
            onclick="btnReadXML_Click" />


Step-2: Write these following lines in the Button's Click Event.
protected void btnReadXML_Click(object sender, EventArgs e)
    {
As I have the XML File in my solution Folder itself. You can give your own path here.
 
        XmlReader xml = XmlReader.Create(Server.MapPath("Customer.xml"), new XmlReaderSettings());
        DataSet dt = new DataSet();
        dt.ReadXml(xml);
        for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
        {
            Response.Write(dt.Tables[0].Rows[i].ItemArray[2].ToString()+"<br/>");
        }
    }

If you debug the page and place a break point at the "dt.ReadXml(xml);", you can see that the DataSet is filled with the values present in the XML FIle.

Output: As we are sending only the Values of 3rd column(Customer Name") to the front, it will print only the customers Name.


Regards,
Prajanuranjan....


No comments:

Post a Comment

Total Pageviews