Friday, July 31, 2009

Data into DataGridView from XML file

In this article we are going to know how to get data in DataGridView from XML file.
now we create an XML file.

Create an XML file:

< ?xml version="1.0" encoding="utf-8" ? >
< bookstore >
< book >
< title > The < /title >
< price > 8.99 < /price >
< genre > autobiograph < /genre >
< publicationdate > 1981 < /publicationdate >
< ISBN > 1-861003-11- < /ISBN >
< PageSize > 500 < /PageSize >
< /book >

< book >
< title > The Confiden < /title >
< price > 11.99 < /price >
< genre > novel < /genre >
< publicationdate > 1967 < /publicationdate >
< ISBN > 0-201-63361- < /ISBN >
< /book >

< book >
< title > The Gorgias < /title >
< price > 9.99 < /price >
< genre > philosophy < /genre >
< publicationdate > 1991 < /publicationdate >
< ISBN > ss1-861001-57- < /ISBN >
< PageSize > 600 < /PageSize >
< /book >
< /bookstore >

Create the window application:
There are the following steps for creating the window application.

Step 1: Create new project.

Step 2: Drag and drop DataGridView control from the toolbox on form, and fill all required properties.

Step 3: Add the following namespace to page (form):

using System.Data.SqlClient;
using System.Xml;

Step 4: Write the code on the form load event.

For Example:

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
DataSet ds = new DataSet("Books DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "Book";
}
}
}

Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates