Pages

Friday 14 September 2012

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

I have already discussed about reading data from XML File in my previous post(http://mydotnetcollections.blogspot.in/2012/09/how-to-read-data-from-xml-file-in.html). Here, I am showing another way of reading data from XML File.
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)
    {
        XmlDataDocument doc = new XmlDataDocument();
        XmlNodeList nodes;
        int count = 0;
        string str = null;


As I have the XML File in my solution Folder itself. You can give your own path here.
 
        FileStream fs = new FileStream(Server.MapPath("Customer.xml"), FileMode.Open, FileAccess.Read);
        doc.Load(fs);
        nodes = doc.GetElementsByTagName("Customers");
        for (int i = 0; i < nodes.Count; i++)
        {
            nodes[i].ChildNodes.Item(0).InnerText.Trim();
            str = nodes[i].ChildNodes.Item(0).InnerText + "|" + nodes[i].ChildNodes.Item(1).InnerText.Trim();
            Response.Write(str+"<br/>");
        }
    }
Now, debug your page and click on the button to print the desired value.

Output: 

Regards,
Prajanuranjan....


No comments:

Post a Comment

Total Pageviews