.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Binding XML to Datagrid

Posted By:Deepika Haridas       Posted Date: December 17, 2009    Points: 10    Category: XML    URL: http://www.dotnetspark.com  
 

Binding XML to Datagrid :

In this code we'll create XML and bind it to Datagrid control.

  1. Add XML file to project (By right click on project ÃƒÂ  Add ÃƒÂ  Add New Item ÃƒÂ  XML file)

2. Now create a XML file (Here I have created a file called Students.xml

 




S0001
Deepika
Ahmedabad


S0002
Swati
Maninagar


S0003
Dhwani
Gandhinagar


S0004
Shweta
Rajkot

3. Notice that when you have an XML file opened, the XML menu appears.

Click on the XML menu. The Create Schema will provide a graphic interface to draw the schema for the XML file. The Validate XML Schema helps you with determining if the XML content validates against a certain XML schema.

4. VS.NET tries to come up with the schema for the XML file based on its content. You can open up the schema file and graphically edit or change its definition by code.

Double-click on the Students.xsd file.

  1. By default, the Create XML Schema option defines all the fields as String. You can change it in this GUI or directly in the code representing this schema.
  2. Now add a button called "Load XML" to a webform which will populate data to gridview from xml file.

 

protected void btnload_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet("Students");
ds.ReadXmlSchema(Server.MapPath(".") + "\\Students.xsd");
ds.ReadXml(Server.MapPath(".") + "\\Students.xml");
GridView1.DataSource = ds;
GridView1.DataBind();
}

 

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend