Tuesday, July 28, 2009

Simple example of XmlDataSource

/*Following is a very simple Example of "How to use XmlDataSource".*/

//FIrst of all in aspx page you have to define controls like this.

sampleXmlDataSource.aspx

<div>
<table border="2">
<tr>
<td>
<asp:XmlDataSource ID="xd1" runat="server" DataFile="~/App_Data/XMLFile8.xml" XPath="Persons/Person"></asp:XmlDataSource>
<asp:DropDownList ID="ddlXml" runat="server" DataSourceID="xd1"
DataValueField="fname"></asp:DropDownList>
</td>
</tr>
</table>
</div>

/*In above code the most important property of xmlDataSource is XPath, for this u ought to know
how to write xPaths. The data will be bound relative to this. xPath*/


//Now in the Code behinde u have to write only following line in page_load

sampleXmlDataSource.aspx.cs

ddlXml.DataBind();

/*Now u must have an XML as a data Source since u want to use XmlDataSource,
here the XmlFile8. xml has been placed under the app_code folder which has format like this.
<Persons>
<Person id="1" fname="Tejal" lname="Vyas"/>
<Person id="2" fname="Sejal" lname="Trivedi"/>
<Person id="3" fname="Hetal" lname="Dave"/>
<Person id="4" fname="Vinita" lname="Raval"/>
<Person id="5" fname="Kavita" lname="Khandhadia"/>
</Persons>
*/

now u run the example u will see a dropdonw list which has all the fname from above xml as data.

No comments:

Post a Comment