In this article we will introduce with XmlDataSource control in ASP.NET using C#.
XmlDataSource Example in ASP.NET using C#
The XmlDataSource is a data source control which is used to bind data with data bound control. The XmlDataSource represents an Xml data. It is used to display both hierarchical and tabular data. The XmlDataSource loads Xml data from Xml file. In this example we use a TreeView and bound data with XmlDataSource.
Employee1.Xml
<?xml version="1.0" encoding="utf-8" ?> <employee> <person name="Manish"> <post>Java Developer</post> <department>Development</department> <mobileno>8785685868</mobileno> <city>Delhi</city> <age>26</age> </person> <person name="Rahul"> <post>Manager</post> <department>Account</department> <mobileno>453543546</mobileno> <city>Banglore</city> <age>30</age> </person> <person name="Ritesh"> <post>PHP Developer</post> <department>Development</department> <mobileno>567457457</mobileno> <city>Mumbai</city> <age>28</age> </person> <person name="Amit"> <post>Engineer</post> <department>Civil</department> <mobileno>435464666</mobileno> <city>Noida</city> <age>35</age> </person> </employee>
There are following steps to configure XmlDataSource in your application.
Step 1. Click on smart tag button and Choose Data Source. Now select Xml file and click OK.
Step 2. Now Browse Data file (Xml file) and click OK.
XmlDataSource.aspx (Design Page):
XmlDataSource.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="XmlDataSource.aspx.cs" Inherits="XmlDataSource" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div> <h2 style="color: Green"> XmlDataSource in ASP.NET 4, C# </h2> <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" ImageSet="BulletedList4" ShowExpandCollapse="False" Width="181px"> <DataBindings> <asp:TreeNodeBinding DataMember="name" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="post" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="department" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="mobileno" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="city" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="age" TextField="#InnerText" /> <asp:TreeNodeBinding DataMember="person" TextField="name" /> </DataBindings> <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" /> <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" /> <ParentNodeStyle Font-Bold="true" /> <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" /> </asp:TreeView> <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XML/Employee1.xml"> </asp:XmlDataSource> </div> </asp:Content>
[ 0 ] Comments