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.
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.
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");
}
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.