Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | Videos | Beginners
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Visual Studio 2005 » Show the data in to DataGridView from the XML file

Show the data in to DataGridView from the XML file

This article shows that: How we can show the data in a DataGridView from the XML file?

Author Rank:
Technologies: .NET Compact Framework, .NET 1.0/1.1, Controls, Windows Forms, XML,Visual Studio 2005
Total downloads : 270
Total page views :  21320
Rating :
 5/5
This article has been rated :  1 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
Book Application.zip
 
ArticleAd
Become a Sponsor



Introduction:

 

In this article we will discuss how we can show the data into 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>

    </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>

    </book>

</bookstore>

   

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

 

Step 1: Create new project.

 

Step 2: Drag DataGridView control from the toolbox and drop on the form, and set the properties of the DataGridView contol.

 

Step 3: Add the following namespace:

 

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";

        }

    }

}

 

Step 5: Build and debug the application. You will see the following output:

 

 

Figure 1: Data in to DataGridView

 

To view the demo of this application:

 

Download zip folder --> right click on that folder --> Click on extract all.

 

Now copy that folder and paste in to virtual directory.

 

You will get an XML file in that folder --> Copy that file and paste in to C drive because in the application we have to pass the path as follows:

 

xmlDatadoc.DataSet.ReadXml("C:\\books.xml");

 

At last open the Microsoft visual studio 2005 and open the project.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Purushottam Rathore
I am working as a Software Developer and has around 2 years of experience in the IT industry and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with, ASP.NET 2.0, SQL Server 2005, C#.Net, AJAX and .NET.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
Book Application.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
VB 2008Shakil2/4/2009
Hi I am a complete beginner to VB2008, and was wondering if anyone could show how to write the same code for VB 2008 Express, If you could let me know that would be good. Shakil
Reply | Email | Delete | Modify | 
DataGridView to/from XML to/from ExcelNate3/5/2009
I'd like to see a Visual Basic 2008 Express version. I need to take a table built in Excel and Export it to XML (pretty easy to do from Excel). Then I need to Import the XML file into Visual Basic for various processing. Then I need to Export it back to XML for later processing by a different Visual Basic program. So a compact, clean and efficient way, such as you have show, to both Import AND Export XML into and out of Visual Basic would be great. Thanks for posting this useful code.
Reply | Email | Delete | Modify | 
VB VersionKees3/21/2009
There is a VB version for those asking that does exactly this here: http://www.depopo.net/content/publish/dataaccess/How_to_import_an_XML_file_into_a_datagridview.shtml

also copied below, as links tend to disappear :)

         ' --- Open a filepicker and let the user select a file
        Dim s_filename As String
        Dim myopenfiledialog As New OpenFileDialog
        myopenfiledialog.ShowDialog()
        s_filename = myopenfiledialog.FileName ' the file selected by the user

        ' --- Read the content of the xml file
        Dim myxmldoc As Xml.XmlDataDocument
        myxmldoc = New Xml.XmlDataDocument
        myxmldoc.DataSet.ReadXml(s_filename)

        ' --- Get the data into a dataset
        Dim mydataset As New DataSet
        mydataset = myxmldoc.DataSet

        ' --- And bind it to the datagridview
        '     Reference the table number by name if you know it, or by number if not
        DataGridView1.DataSource = mydataset.DefaultViewManager
        DataGridView1.DataMember = mydataset.Tables(1).ToString
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved