C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » XML »

Bind XML to DataGrid


Posted Date: 21 Oct 2009    Resource Type: Code Snippets    Category: XML
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 25



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

<?xml version="1.0" encoding="utf-8" ?>

<Students>

<student>

<ID>S0001</ID>

<Name>Deepika</Name>

<City>Ahmedabad</City>

</student>

<student>

<ID>S0002</ID>

<Name>Swati</Name>

<City>Maninagar</City>

</student>

<student>

<ID>S0003</ID>

<Name>Dhwani</Name>

<City>Gandhinagar</City>

</student>

<student>

<ID>S0004</ID>

<Name>Shweta</Name>

<City>Rajkot</City>

</student>

</Students>

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();

}



Attachments

  • Bind XML to DataGrid (34266-21517-BindXML.rar)

    For more details, visit http://angeldeeps.blogspot.com/2009/10/bind-xml-to-datagrid.html



  • Responses


    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    XML  .  Web applications  .  DataGrid  .  Bind XML to DataGrid  .  Bind XML  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Add Birth Day Details in XML File
    Previous Resource: Reading xml document from html page.
    Return to Discussion Resource Index
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use