Pages

Ads 468x60px

For New Update Use this Link http://dotnethubs.blogspot.in/ Offer for you

.

Showing posts with label xml file. Show all posts
Showing posts with label xml file. Show all posts

Saturday 12 October 2013

How to get xml data into grid view in asp.net

How to get xml data into grid view
Introduction :- In this article i have explain how to show XML file data in asp.net grid-view .

Description :- In  my previous post i have explained How to Generating XML file From MySQL database 
Create and read XML file in asp.net , Read data from XML file in asp.net , now i have explain how to show XML file data in grid view  using   asp.net

Step 1 :- Create a new asp.net web site.
Step 2:- Add a new page in your project and give the name to the page gridxml.aspx..
Step 3 :- Add  one xml file and write something like this which you want to show on gridview page of asp.net as show below.

Note  - This is a Employees.xml file already exist in your project.


<?xml version="1.0" standalone="yes"?>
<Employees>
  <Employee>
    <EmployeeID>Chitranjan</EmployeeID>
    <CompanyName>abc informatics</CompanyName>
    <ContactName>chintu </ContactName>
    <ContactTitle>Developer</ContactTitle>
    <Address>ganraj</Address>
    <City>Indore</City>
    <PostalCode>452001</PostalCode>
    <Country>India</Country>
    <Phone>00000000</Phone>
    <Fax>4545499</Fax>
  </Employee>
  <Employee>
    <EmployeeID>Aman</EmployeeID>
    <CompanyName>Y N Info</CompanyName>
    <ContactName>Aman</ContactName>
    <ContactTitle>Artiest</ContactTitle>
    <Address>Boriwali east</Address>
    <City>Pune</City>
    <PostalCode>3436</PostalCode>
    <Country>India</Country>
    <Phone>9549544345</Phone>
    <Fax>559935</Fax>
  </Employee>
  <Employee>
    <EmployeeID>Rahul</EmployeeID>
    <CompanyName>Gold Main</CompanyName>
    <ContactName>Hot</ContactName>
    <ContactTitle>Designer</ContactTitle>
    <Address>dfdf</Address>
    <City>bBombay</City>
    <PostalCode>5434546</PostalCode>
    <Country>India</Country>
    <Phone>54545</Phone>
  </Employee>
  <Employee>
    <EmployeeID>Ronak</EmployeeID>
    <CompanyName>Yeswant Niwas</CompanyName>
    <ContactName>Golo</ContactName>
    <ContactTitle>Producer</ContactTitle>
    <Address>345 Alock Nagar</Address>
    <City>Bhopal</City>
    <PostalCode>554677</PostalCode>
    <Country>India</Country>
    <Phone>5656</Phone>
    <Fax>0909ASD</Fax>
  </Employee>
  <Employee>
    <EmployeeID>Ravi</EmployeeID>
    <CompanyName>DFF</CompanyName>
    <ContactName>LJ</ContactName>
    <ContactTitle>PPOIU</ContactTitle>
    <Address>79879 JKJK</Address>
    <City>ABC</City>
    <PostalCode>6656DF</PostalCode>
    <Country>RFF</Country>
    <Phone>54554FGGG</Phone>
    <Fax>GGG555</Fax>
  </Employee>
  <Employee>
    <EmployeeID>JJ8</EmployeeID>
    <CompanyName>HJKGJ8</CompanyName>
    <ContactName>GGH55</ContactName>
    <ContactTitle>New Dehli</ContactTitle>
    <Address>Rambag</Address>
    <City>ASD</City>
    <PostalCode>87GG</PostalCode>
    <Country>India</Country>
    <Phone>7667654</Phone>
    <Fax>FGH45</Fax>
  </Employee>
  <Employee>
    <EmployeeID>Sumit</EmployeeID>
    <CompanyName>Sumo tech</CompanyName>
    <ContactName>Sumo</ContactName>
    <ContactTitle>Manager</ContactTitle>
    <Address>656 FFH</Address>
    <City>Strasbourg</City>
    <PostalCode>8788</PostalCode>
    <Country>Noida</Country>
    <Phone>665-778-545</Phone>
    <Fax>545.54.55</Fax>
  </Employee>
 
</Employees>

 Step 4 :- open the gridxml.aspx file and write the code as show below.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.BindGrid();
        }
    }

    private void BindGrid()
    {
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(Server.MapPath("~/Employees.xml"));
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }

    protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }
}

Note -: Source code also available in vb.net

Download sample code attached



Friday 14 June 2013

read data from xml file in asp.net

read data from xml file in asp.net
Categories:-aspdotnet interview question and answer , create wcf restful web service in asp.net

Introduction :-  

Here i am explain how i can retrieve  data from XML  file in asp.net. show the data of XML file on the web form.

Description :-

XML stands for Extensible Markup Language.XML is a markup language much like HTML. Using xml we can store the data and we can easily retrieve and display the data without using database in our application.Using XML no need to connect any database. If  we  want to display data dynamically then we use the XML file.On the XML file we can Perform the read write and update  operation .
Now  i will explain how i can read data from xml in asp.net.

 Design your aspx page as show below



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlpageread.aspx.cs" Inherits="xml_file_xmlpageread" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1 {
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="style1">
  
        Enter ID
        <asp:TextBox ID="txt_id" runat="server"></asp:TextBox>
&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
        <br />
        <br />
        <br />
  
    </div>
    </form>
</body>
</html>


In the above aspx page design i have taken a one textobx and on button .Textbox use for insert the Employee id  which present in xml file and press submit button get the detail related to this employee id.

code behind file as show below



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class xml_file_xmlpageread : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            string empid1 = txt_id.Text.ToString();
            if (empid1 != null)
            {
                //Response.Write("<br>XML file searching using asp.net<br><br>");
                XmlDocument doc = new XmlDocument();

                string xmlFile = System.Web.HttpContext.Current.Server.MapPath("~/XMLFile.xml");
                doc.Load(xmlFile);
                int empid = int.Parse(empid1);// convert the empid1 to int

                XmlNodeList xmlnode = doc.GetElementsByTagName("blockid"); //this is tag of xml file
                for (int i = 0; i < xmlnode.Count; i++)
                {

                    XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
                    if (int.Parse(xmlattrc[0].Value) == empid)
                    {
                        Response.Write("<br>Employee-ID: " + xmlattrc[0].Value);
                        Response.Write("<br>First Name: " + xmlnode[i].ChildNodes[0].InnerText);     
                        Response.Write("<br>");
                    }
                }
            }
        }
        catch (Exception exp)
        {
          
        }
    }
}

Xml Form at run time as show below

Enter the Employee Id Into the textbox and press submit button the display the output as shown below
figure
Download sample code attached






 

..




New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result