 |
Operations on XML Elements |
|
|
|
So far, to create an XML element, we were directly typing in
a file. When such a file had been created and saved, it was ready to be
processed. Here is an example of a file named videos.xml:
|
<?xml version="1.0" encoding="utf-8" ?>
<videos>
<title>The Distinguished Gentleman</title>
</videos>
In some applications, you will want the user to provide you with the
necessary value(s) to create an element.
|
Fortunately, the XmlDocument, the XmlNode,
and the
XmlElement classes provide all the necessary properties and methods to perform
the routine operations of an XML file, an element, or a node. The operations
include locating a node, adding a new element, or deleting a node.
Before performing an operation, you will usually need to
decide in what section of the file you want the action to be applied. As it
happens, you have the root node, a particular node inside the document, parent of a
node, the sibling of a node, etc. To get to a node, you will usually first get a
reference to its XmlElement. To do this, you can declare an
XmlElement variable and initialize it with that reference.
|
|
|
Practical
Learning: Introducing operations on XML Elements
|
|
- Start Microsoft Visual C# and create a Console
Application named SolasPropertyRental1
- To add a new form to the application, in the Solution Explorer, right-click
SolasPropertyRental1 -> Add -> Windows Form...
- Set the Name to Tenants and click Add
- From the Toolbox, add a ListView to the form
- While the new list view is still selected, in the Properties window, click
the ellipsis button of the Columns field and create the columns as follows:
| (Name) |
Text |
TextAlign |
Width |
| colAccountNumber |
Account # |
|
65 |
| colFullName |
Full Name |
|
120 |
| colMaritalStatus |
Marital Status |
|
85 |
| colPhoneNumber |
Phone # |
Center |
85 |
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| ListView |
|
lvwTenants |
Anchor: Top, Bottom, Left, Right
FullRowSelect: True
GridLines: True
View: Details |
| Button |
New Tenant... |
btnNewTenant |
Anchor: Bottom, Right |
| Button |
Close |
btnClose |
Anchor: Bottom, Right |
|
- Double-click an unoccupied area of the Tenants form
- Implement the Load event as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace SolasPropertyRental1
{
public partial class Tenants : Form
{
public Tenants()
{
InitializeComponent();
}
void ShowTenants()
{
string Filename = @"C:\Solas Property Rental\tenants.xml";
XmlDocument docTenants = new XmlDocument();
if (File.Exists(Filename))
{
lvwTenants.Items.Clear();
docTenants.Load(Filename);
XmlElement elmTenant = docTenants.DocumentElement;
XmlNodeList lstTenants = elmTenant.ChildNodes;
foreach (XmlNode node in lstTenants)
{
ListViewItem lviTenant = new ListViewItem(node.FirstChild.InnerText); // Account Number
lviTenant.SubItems.Add(node.FirstChild.NextSibling.InnerText); // Full Name
lviTenant.SubItems.Add(node.FirstChild.NextSibling.NextSibling.InnerText); // Phone Number
lviTenant.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText); // Marital Status
lvwTenants.Items.Add(lviTenant);
}
}
}
private void Tenants_Load(object sender, EventArgs e)
{
ShowTenants();
}
}
}
|
- To add another form to the application, in the Solution Explorer, right- click
SolasPropertyRental1 -> Add ->
Windows Form...
- Set the Name to TenantEditor and click Add
- Design the form as follows:
 |
| Control |
Text |
Name |
Properties |
| Label |
&Account #: |
|
|
| MaskedTextBox |
|
txtAccountNumber |
Mask: 00-00-00
Modifiers: public |
| Label |
&Full Name: |
|
|
| TextBox |
|
txtFullName |
Modifiers: public |
| Label |
Marital Status: |
|
|
| ComboBox |
|
txtMaritalStatus |
Modifiers: public
Items:
Single
Widow
Married
Divorced
Separated |
| Label |
&Phone #: |
|
|
| MaskedTextBox |
|
txtPhoneNumber |
Mask: (999) 000-0000
Modifiers: public |
| Button |
OK |
btnOK |
DialogResult: OK |
| Button |
Cancel |
btnCancel |
DialogResult: Cancel |
| Form |
|
|
AcceptButton: btnOK
CancelButton: btnCancel
FormBorderStyle: FixedDialog
MaximizeBox: False
MinimizeBox: False
ShowInTaskbar: False |
|
- To add a new form to the application, in the Solution Explorer, right- click
SolasPropertyRental1 -> Add ->
Windows Form...
- Set the Name to RentalProperties and press Enter
- From the Toolbox, add a ListView to the form
- While the new list view is still selected, in the Properties window, click
the ellipsis button of the Columns field and create the columns as follows:
| (Name) |
Text |
TextAlign |
Width |
| colPropertyCode |
Prop Code |
|
65 |
| colPropertyType |
Property Type |
|
85 |
| colBedrooms |
Bedrooms |
Right |
65 |
| colBathrooms |
Bathrooms |
Right |
65 |
| colMonthlyRent |
Monthly Rent |
Right |
75 |
| colStatus |
Status |
|
65 |
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| ListView |
|
lvwProperties |
View: Details
GridLines: True
FullRowSelect: True
Anchor: Top, Bottom, Left, Right |
| Button |
New Property... |
btnNewProperty |
Anchor: Bottom, Right |
| Button |
Close |
btnClose |
Anchor: Bottom, Right |
|
- Double-click an unoccupied area of the Rental Properties form and implement
the Load event as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace SolasPropertyRental1
{
public partial class RentalProperties : Form
{
public RentalProperties()
{
InitializeComponent();
}
void ShowProperties()
{
XmlDocument docProperties = new XmlDocument();
string Filename = @"C:\Solas Property Rental\properties.xml";
if (File.Exists(Filename))
{
lvwProperties.Items.Clear();
docProperties.Load(Filename);
XmlElement elmProperty = docProperties.DocumentElement;
XmlNodeList lstProperties = elmProperty.ChildNodes;
foreach (XmlNode node in lstProperties)
{
ListViewItem lviProperty = new ListViewItem(node.FirstChild.InnerText); // Property Code
lviProperty.SubItems.Add(node.FirstChild.NextSibling.InnerText); // Property Type
lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.InnerText); // Bedrooms
lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText); // Bathrooms
lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Monthly Rent
lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText); // Status
lvwProperties.Items.Add(lviProperty);
}
}
}
private void RentalProperties_Load(object sender, EventArgs e)
{
ShowProperties();
}
}
}
|
- To add another form to the application, in the Solution Explorer, right-
click SolasPropertyRental1 -> Add -> Windows Form...
- Set the Name to PropertyEditor and click Add
- Design the form as follows:
 |
| Control |
Text |
Name |
Properties |
| Label |
Property Code: |
|
|
| MaskedTextBox |
|
txtPropertyCode |
Mask: 000-000
Modifiers: Public |
| Button |
OK |
btnOK |
DialogResult: OK |
| Label |
Property Type: |
|
|
| ComboBox |
|
cbxPropertyTypes |
Modifiers: Public
Items: Unknown
Apartment
Townhouse
Single Family |
| Button |
Cancel |
btnCancel |
DialogResult: Cancel |
| Label |
Bedrooms: |
|
|
| TextBox |
0 |
txtBedrooms |
TextAlign: Right
Modifiers: Public |
| Label |
Bathrooms: |
|
|
| TextBox |
0.00 |
txtBathrooms |
TextAlign: Right
Modifiers: Public |
| Label |
Monthly Rent: |
|
|
| TextBox |
0.00 |
txtMonthlyRent |
TextAlign: Right
Modifiers: Public |
| Label |
Occupancy Status: |
|
|
| ComboBox |
Unknown |
cbxStatus |
Modifiers: Public
Items:
Unknown
Available
Occupied
Needs Repair |
| Form |
|
|
AcceptButton: btnOK
CancelButton: btnCancel
FormBorderStyle: FixedDialog
MaximizeBox: False
MinimizeBox: False
ShowInTaskbar: False |
|
- To add a new form to the application, in the Solution Explorer, right-click
SolasPropertyRental1 -> Add -> Windows Form...
- Set the Name to RentalAllocation and click Add
- From the Toolbox, add a ListView to the form
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| Label |
Rent Allocation |
|
AutoSize: False
BackColor: Gray
BorderStyle: FixedSingle
ForeColor: White
TextAlign: MiddleLeft |
| Label |
Allocation Code: |
|
|
| MaskedTextBox |
|
txtAllocationCode |
Mask: 0000-9999
Modifiers: Public |
| Label |
Date Allocated: |
|
|
| DateTimePicker |
|
dtpDateAllocated |
Format: Short
Modifiers: Public |
| Label |
Tenant |
|
AutoSize: False
BackColor: Gray
BorderStyle: FixedSingle
ForeColor: White
TextAlign: MiddleLeft |
| Label |
Property |
|
AutoSize: False
BackColor: Gray
BorderStyle: FixedSingle
ForeColor: White
TextAlign: MiddleLeft |
| |
|
|
|
| Label |
Account #: |
|
|
| MaskedTextBox |
|
txtTenantAcntNbr |
Mask: 00-00-00
Modifiers: Public |
| Label |
Property #: |
|
|
| MaskedTextBox |
|
txtPropertyCode |
Mask: 000-000
Modifiers: Public |
| Label |
Tenant Name |
|
|
| TextBox |
|
txtTenantName |
Modifiers: Public |
| Label |
Property Type: |
|
|
| TextBox |
|
txtPropertyType |
Modifiers: Public |
| Label |
Marital Status: |
|
|
| TextBox |
|
txtMaritalStatus |
Modifiers: Public |
| Label |
Monthly Rent: |
|
|
| TextBox |
|
txtMonthlyRent |
|
| Label |
Allocation Evaluation |
|
AutoSize: False
BackColor: Gray
BorderStyle: FixedSingle
ForeColor: White
TextAlign: MiddleLeft |
| Label |
Contract Length: |
|
|
| ComboBox |
|
cbxContractLength |
Items:
Monthly
3 Months
6 Months
12 Months
Modifiers: Public |
| Label |
Rent Start Date: |
|
|
| DateTimePicker |
|
dtpRentStartDate |
Modifiers: Public |
| Button |
OK |
btnOK |
DialogResult: OK |
| Button |
Close |
btnClose |
DialogResult: Cancel |
| Form |
|
|
AcceptButton: btnOK
CancelButton: btnClose
FormBorderStyle: FixedDialog
MaximizeBox: False
MinimizeBox: False
ShowInTaskbar: False |
|
- On the form, click the Account # text box and, in the Properties window,
double-click Leave
- Implement the event as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace SolasPropertyRental1
{
public partial class RentalAllocation : Form
{
public RentalAllocation()
{
InitializeComponent();
}
private void txtTenantAcntNbr_Leave(object sender, EventArgs e)
{
string Filename = @"C:\Solas Property Rental\tenants.xml";
XmlDocument docTenants = new XmlDocument();
if (File.Exists(Filename))
{
docTenants.Load(Filename);
XmlElement elmTenant = docTenants.DocumentElement;
XmlNodeList lstTenants = elmTenant.ChildNodes;
bool TenantFound = false;
for (int i = 0; i < lstTenants.Count; i++)
{
XmlNode node = lstTenants[i];
if (node.FirstChild.InnerText == txtTenantAcntNber.Text)
{
TenantFound = true;
txtTenantName.Text = node.FirstChild.NextSibling.InnerText;
txtMaritalStatus.Text = node.FirstChild.NextSibling.NextSibling.InnerText;
}
}
if (TenantFound == false)
{
MessageBox.Show("There is no tenant with that account number");
txtTenantAcntNber.Text = "";
}
}
else
MessageBox.Show("There is no list of tenants to check.");
}
}
}
|
- Return to the Rent Allocation form and click the Property Code text box
- In the Events section of the Properties window, double-click Leave
- Implement the event as follows:
private void txtPropertyCode_Leave(object sender, EventArgs e)
{
string Filename = @"C:\Solas Property Rental\properties.xml";
XmlDocument docProperties = new XmlDocument();
if (File.Exists(Filename))
{
docProperties.Load(Filename);
XmlElement elmProperty = docProperties.DocumentElement;
XmlNodeList lstProperties = elmProperty.ChildNodes;
bool PropertyFound = false;
for (int i = 0; i < lstProperties.Count; i++)
{
XmlNode node = lstProperties[i];
if (node.FirstChild.InnerText == txtPropertyCode.Text)
{
PropertyFound = true;
txtPropertyType.Text = node.FirstChild.NextSibling.InnerText;
txtMonthlyRent.Text = node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText;
}
}
if (PropertyFound == false)
{
MessageBox.Show("There is no Property with that code");
txtPropertyType.Text = "";
}
}
else
MessageBox.Show("There is no list of properties to check.");
}
|
- To add a new form to the application, in the Solution Explorer, right- click
SolasPropertyRental1 -> Add ->
Windows Form...
- Set the Name to RentalAllocations and press Enter
- From the Toolbox, add a ListView to the form
- While the new list view is still selected, in the Properties window, click
the ellipsis button of the Columns field and create the columns as follows:
| (Name) |
Text |
TextAlign |
Width |
| colAllocationCode |
Alloc Code |
|
65 |
| colDateAllocated |
Date Allocated |
Center |
85 |
| colTenantAccount |
Tenant # |
Center |
65 |
| colTenantName |
Tenant Name |
|
100 |
| colPropertyCode |
Prop Code |
Center |
65 |
| colPropertyType |
Prop Type |
|
75 |
| colContractLength |
Contract Length |
|
88 |
| colRentStartDate |
Rent Start Date |
Center |
88 |
| colMonthlyRent |
Monthly Rent |
Right |
76 |
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| ListView |
lvwAllocations |
View: Details GridLines: True FullRowSelect: True Anchor: Top, Bottom, Left, Right |
|
| |