Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
ANTS Performance Profiler 6.0
 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:
Total page views :  46045
Total downloads :  606
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Book Application.zip
 
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
 About the author
 
Purushottam Rathore
I am working as a Software Developer and has 3 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008 .
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.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
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.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments
VB 2008 by Shakil On February 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 Excel by Nate On March 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 Version by Kees On March 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 | 
XML format is change so output is not come ... by Ankit On December 19, 2009
Here My format of XM is changed so facing a problem to get appropriate output
Please help............

<EmployeeDetail>
    <EmployeeID>
          <EmpId>101</EmpId>
          <EmpId>202</EmpId>
      </EmployeeID>
    <EmployeeName>
          <EmpName>Jai</EmpName>
          <EmpName>Anurag</EmpName>
      </EmployeeName>
    <EmpAddress>
        <EmpAdd>Bangalopre</EmpAdd>
        <EmpAdd>Bangalopre</EmpAdd>    
    </EmpAddress>
</EmployeeDetail>
 
Reply | Email | Delete | Modify | 
XML format is change so output is not come ... by Ankit On December 21, 2009
Hi Guys,

My problem is still not solved....

Suppose I have an XML document like:-

<EmployeeDetail>
    <EmployeeID>
          <EmpId>101</EmpId>
          <EmpId>202</EmpId>
      </EmployeeID>
    <EmployeeName>
          <EmpName>Jai</EmpName>
          <EmpName>Anurag</EmpName>
      </EmployeeName>
    <EmpAddress>
        <EmpAdd>Bangalopre</EmpAdd>
        <EmpAdd>Bangalopre</EmpAdd>   
    </EmpAddress>
</EmployeeDetail>
 

 
 And these detail I have to show in DATAGRID.........
So please tell me the solution
Reply | Email | Delete | Modify | 
Query!!Urgent by Archana On January 5, 2010
Hi

So now the grid is having 3 rows added. now if we need to add one more row from the xml . How do we do that? Could you please guide me in this?
Reply | Email | Delete | Modify | 
thnx by Arslan On May 13, 2010
this code is not working. when i put in this code. it creates problem in accessing the xml file....
arslan
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.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.