Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, March 08, 2012

Sample Code Retrieving Data from XML File

OutPut:-

HI

I am using Repeater as well as gridview in this Post.

XML File Structure I Have Created
<?xml version="1.0" encoding="utf-8" ?>
<Contacts>
  <Contact>
  <Fname>Prasad</Fname>
  <City>HYd</City>
  <Telno>1234</Telno>
  </Contact>
 
  <Contact>
  <Fname>Prasad1</Fname>
  <City>HYd</City>
  <Telno>1234</Telno>
    </Contact>

  <Contact>
    <Fname>Prasad2</Fname>
    <City>HYd</City>
    <Telno>1234</Telno>
    </Contact>

  <Contact>
    <Fname>Prasad3</Fname>
    <City>HYd</City>
    <Telno>1234</Telno>
  </Contact>

  <Contact>

    <Fname>Prasad4</Fname>
    <City>HYd</City>
    <Telno>1234</Telno>
  </Contact>

  <Contact>

    <Fname>Prasad5</Fname>
    <City>HYd</City>
    <Telno>1234</Telno>
  </Contact>
</Contacts>

ASpX Source Code
  <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
        <table width="100%" cellpadding="0" cellspacing="0">
        <asp:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
        <tr bgcolor="yellow" style="color:Red;font-weight:bold;"><td>Fname</td><td>City</td><td>Telno</td></tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr bgcolor="green" style="color:White;">
        <td><%#DataBinder.Eval(Container.DataItem, "Fname")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "City")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "Telno")%></td>
        </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
        <tr bgcolor="LightBlue" style="color:White;">
        <td><%#DataBinder.Eval(Container.DataItem, "Fname")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "City")%></td>
        <td><%#DataBinder.Eval(Container.DataItem, "Telno")%></td>
        </tr>
        </AlternatingItemTemplate>
        </asp:Repeater>
        </table>
Binding Data from XML File
DataSet dSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = getContacts();
        GridView1.DataBind();

        Repeater1.DataSource = getContacts();
        Repeater1.DataBind();
    }
    public DataSet getContacts()
    {
        dSet = new DataSet();
        dSet.ReadXml(Server.MapPath("Contacts.xml"));
        return dSet;
    }

No comments:

Post a Comment