Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++/CLI

Using DataGrids to display records in XML databases in VC++ 2008

1.80/5 (4 votes)
1 Sep 2008CPOL1 min read 1   481  
This application will display XML data in a table format and write tabular data to XML documents.

DataGrids_in_XML

Introduction

In this article, I will show how to use Visual C++ 2008 to represent your XML data in tabular format. Many examples use C#, and amateur developers of C++/CLI applications may not know how to translate the code.

Background

XML databases are crucial, especially for Web applications and portability. Further still, they are quite useful for low RAM computing systems. The .NET platform contains predefined functions that will enable us to read directly and write to an XML database from a DataGrid contained in a Windows form.

Code

Use the XML file "FILMS.xml" provided in the zip archive.

Remember to use the following namespaces; otherwise, XML methods will not be recognized.

C++
using namespace System::Data::SqlClient;
using namespace System::Xml;

I have chosen to hide the load button after the display of the records is complete, because pressing it twice will add onto the already existing data. I am still trying to get a way round this... I will let you know!

Please remember to replace:

"D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml"

with your own path that gets to the XML file.

C++
XmlDataDocument mydoc; 

private: System::Void LoadDataButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
    try 
    {
        mydoc.DataSet->ReadXml("D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml");

        XMLDataGridView->DataSource = mydoc.DataSet->DefaultViewManager;
        XMLDataGridView->DataMember = "FILM";
        LoadDataButton->Hide();
    }
    catch(Exception^ ex)
    {
        MessageBox::Show("Error:"+ ex->Message);
    }
}

private: System::Void SaveDataButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
    mydoc.DataSet->WriteXml("D:\\APPLICATION DEVELOPMENT\\XML\\FILMS.xml");
    //this function will get data from the Grid and Update in well formed XML format
}

Points of Interest

WriteXML auto-translates the edited table to well formed XML! So, you don't have to keep on making new tags and all in your XML document... Just put all your data in the table and click "Save"!!!

Note

I am still trying to get it to accept records from Excel such that we can copy and paste at a go and save in XML in just a second.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)