- Start Microsoft Visual C++ .Net or MS Visual Studio .Net and create
a new Windows Forms Application named IntroXML1 with its default
form
- To create an XML file, on the main menu, click Project -> Add New
Item...
- In the Templates list, click XML File (.xml)
- In the Name box, replace the string with employees
- Click Open
- Click the empty line under the first. Type <fullrecord>
- Notice that as soon as you type the > sign, the closing tag is
created. Complete the file as follows:
<?xml version="1.0" encoding="utf-8"?>
<fullrecord>
<employee>
<firstname>Sylvie</firstname>
<lastname>Aronson</lastname>
<salary>25.64</salary>
<gender>Female</gender>
</employee>
<employee>
<firstname>Bertrand</firstname>
<lastname>Yamaguchi</lastname>
<salary>16.38</salary>
<gender>Male</gender>
</employee>
<firstname>Anselme</firstname>
<lastname>Bean</lastname>
<salary>22.82</salary>
<gender>Male</gender>
<employee>
<firstname>Mauricette</firstname>
<lastname>Thomas</lastname>
<salary>18.35</salary>
<gender>Female</gender>
</employee>
<employee>
<firstname>Hermine</firstname>
<lastname>Gray</lastname>
<salary>12.75</salary>
<gender>Female</gender>
</employee>
</fullrecord>
|
- Display the form
- From the Windows Forms section of the Toolbox, click DataGrid and click the form:
- Right-click the form and click View Code
- To use a data set, declare a DataSet pointer in the form outside the
constructor:
#pragma once
namespace IntroXML1
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::DataGrid * dataGrid1;
DataSet *dsEmployees;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
. . .
}
};
}
|
- Display the form
- Double-click any empty area on the form and implement its Load event
as follows:
private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
this->dsEmployees = new DataSet(S"employees");
this->dsEmployees->ReadXml(S"employees.xml");
this->dataGrid1->DataSource = this->dsEmployees;
this->dataGrid1->DataMember = S"employee";
}
|
- Test the application
- Close the form
|