Home

Operations on XML Elements

Fundamental Operations

Introduction

To create an XML element, you can directly type the XML code in a document. When the file is saved, it is 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 LearningPractical Learning: Introducing operations on XML Elements

  1. Start Microsoft Visual Studio if necessary and open the LambdaSquareApartments2 application from the previous lesson.
    To create a new dialog box, on the main menu, click PROJECT -> Add Windows Form...
  2. Set the Name to ApartmentEditor and press Enter
  3. Design the form as follows:
     

    Lambda Square Apartments - New Apartment

    Control (Name) Text Other Properties
    Label Label   Unit Code:  
    TextBox Text Box txtUnitCode   Modifiers: Public
    Label Label   Apartment #:  
    TextBox Text Box txtApartmentNumber   Modifiers: Public
    Label Label   Bedrooms:  
    TextBox Text Box txtBedrooms   Modifiers: Public
    TextAlign: Right
    Label Label   Bathrooms:  
    TextBox Text Box txtBathrooms   Modifiers: Public
    TextAlign: Right
    Label Label   Monthly Rate:  
    TextBox Text Box txtMonthlyRate   Modifiers: Public
    TextAlign: Right
    Label Label   Security Deposit:  
    TextBox Text Box txtSecurityDeposit   Modifiers: Public
    TextAlign: Right
    Label Label   Occupancy Status:  
    ComboBox Combo Box cbxOccupanciesStatus   Modifiers: Public
    TextAlign: Right
    Items:
    Other
    Available
    Occupied
    Not Ready
    Needs Repair
    Button Button &OK btnOK DialogResult: OK
    Button Button &Cancel btnCancel DialogResult: Cancel
    Form Property Value FormBorderStyle FixedDialog StartPosition CenterScreen AcceptButton btnOK CancelButton btnCancel MaximizeBox False MinimizeBox False ShowInTaskbar False

Appending an Element

To assist with programmatically creating a new element, the XmlDocument class provides the CreateElement() method that is overloaded with three versions. One of the versions uses the following syntax:

public XmlElement CreateElement(string name);

To create a new element, call this method and pass it the name of the element. For example, imagine you want to add a new Title element to the above file. You would start with code 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 videoCollection1
{
    public partial class Exercise : Form
    {
        public Exercise()
        {
            InitializeComponent();
        }

        private void btnDocument_Click(object sender, EventArgs e)
        {
            string Filename = "videos.xml";
            XmlDocument DOMVideos = new XmlDocument();

            if (File.Exists(Filename))
            {
                DOMVideos.Load(Filename);

                XmlElement elmNew = DOMVideos.CreateElement("Title");
            }
        }
    }
}

In order to add the new element to the file, you must specify its position in the tree: whether it would be the first or the last node, whether you want to position it before or after a node of your choice. For example, if you want to add a new Title element to the above file, it would be considered a child of the root, that is, a child of the XmlDocument.DocumentElement property. In the previous lesson, we learned how to get a reference to the root node.

To support the positions of existing nodes, the XmlNode class, which is the ancestor of all XML nodes of the .NET Framework, provides various appropriate methods. One of these methods is AppendChild(), which is used to add an element as the last child of its parent. The syntax of the XmlNode.AppendChild() method is:

public virtual XmlNode AppendChild(XmlNode newChild);

When calling this method, pass the XmlNode object you had previous created. After adding the node, if you want the file to keep it, you should save it. Here is an example:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmRoot = DOMVideos.DocumentElement;
        XmlElement elmNew = DOMVideos.CreateElement("title");
        elmRoot.AppendChild(elmNew);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos />

or:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <title />
</videos>

Notice that the newly added element is empty.

Adding the Inner Text of an Element

Suppose you have the following videos.xml file:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
</videos>

Imagine that you want to add a video element. You have a choice of adding one, more than one, or all child elements of the video node. To perform this operation, one solution you can use is to "build" all child elements of the video element, then add the node as a whole.

In the previous lesson, we saw that the XmlNode.InnerXml property comprises a node, its markup, its children and their markup. This means that you can create the child node(s) with its (their) markup(s) as a string and assign that string to an XmlNode.InnerXml string. To do this, you would need the set version of the InnerXml property. It is declared as follows:

public virtual string InnerXml{get; set;}

Here is an example that adds a complete new video node to the above XML file:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmXML = DOMVideos.CreateElement("video");
        string strNewvideo = "<title>Other People's Money</title>" +
                             "<director>Alan Brunstein</director>" +
                             "<length>114 Minutes</length>" +
                             "<format>VHS</format>" +
                             "<rating>PG-13</rating>";

        elmXML.InnerXml = strNewvideo;
        DOMVideos.DocumentElement.AppendChild(elmXML);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>Other People's Money</title>
    <director>Alan Brunstein</director>
    <length>114 Minutes</length>
    <format>VHS</format>
    <rating>PG-13</rating>
  </video>
</videos>

Practical LearningPractical Learning: Introducing Operations on XML Elements

  1. Display the Employees.cs [Design] tab and double-click the New Employee button
  2. Implement the Click event as follows:
    private void btnNewEmployee_Click(object sender, EventArgs e)
    {
        string EmployeeNumber;
        string FirstName, LastName, Title;
    
        // This is the XML file that holds the list of employees
        string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Employees.xml";
        // This is the dialog box from where an employee record is created
        EmployeeEditor editor = new EmployeeEditor();
    
        // Display the Employee Editor dialog box and find out if the 
        // user clicked OK after filling its controls
        if (editor.ShowDialog() == DialogResult.OK)
        {
            // We will need a reference to the XML document
            XmlDocument xdEmployee = new XmlDocument();
    
            // Find out if the file exists already
            // If it doesn't, then create it
            if (!File.Exists(strFileName))
            {
                xdEmployee.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                    "<Employees></Employees>");
                xdEmployee.Save(strFileName);
            }
    
            // At this time, we have an Employees.xml file. Open it
            xdEmployee.Load(strFileName);
    
            // Get a reference to the root node
            XmlElement nodRoot = xdEmployee.DocumentElement;
    
            // Prepare the values of the XML element
            EmployeeNumber = editor.txtEmployeeNumber.Text;
            FirstName = editor.txtFirstName.Text;
            LastName = editor.txtLastName.Text;
            Title = editor.txtTitle.Text;
    
            // Create an element named Tenant
            XmlElement elmXML = xdEmployee.CreateElement("Employee");
            // Create the XML code of the child element of Employee
            string strNewEmployee = "<EmployeeNumber>" + EmployeeNumber + "</EmployeeNumber>" +
                                    "<FirstName>" + FirstName + "</FirstName>" +
                                    "<LastName>" + LastName + "</LastName>" +
                                    "<Title>" + Title + "</Title>";
            elmXML.InnerXml = strNewEmployee;
            // Append the new element as a child of employees
            xdEmployee.DocumentElement.AppendChild(elmXML);
    
            // Save the XML file
            xdEmployee.Save(strFileName);
            // Since the content of the XML file has just been changed,
            // re-display the list of employees
            ShowEmployees();
        }
    }
  3. Return to the Employees form and double-click the Close button
  4. Implement its Click event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  5. Display the Apartments form and double-click the New Apartment button
  6. Implement the Click event as follows:
    private void btnNewApartment_Click(object sender, EventArgs e)
    {
        int Bedrooms;
        float Bathrooms;
        double MonthlyRate, SecurityDeposit;
        string UnitCode, ApartmentNumber, OccupancyStatus;
        XmlDocument docApartment = new XmlDocument();
        ApartmentEditor editor = new ApartmentEditor();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Apartments.xml";
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            if (!File.Exists(strFileName))
            {
                docApartment.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                    "<Apartments></Apartments>");
                docApartment.Save(strFileName);
            }
    
            // Open the XML file
            docApartment.Load(strFileName);
    
            // Get a reference to the root node
            XmlElement nodRoot = docApartment.DocumentElement;
    
            UnitCode = editor.txtUnitCode.Text;
            ApartmentNumber = editor.txtApartmentNumber.Text;
            Bedrooms = int.Parse(editor.txtBedrooms.Text);
            Bathrooms = float.Parse(editor.txtBathrooms.Text);
            MonthlyRate = double.Parse(editor.txtMonthlyRate.Text);
            SecurityDeposit = double.Parse(editor.txtSecurityDeposit.Text);
            OccupancyStatus = editor.cbxOccupanciesStatus.Text;
    
            XmlElement elmXML = docApartment.CreateElement("Apartment");
            string strNewApartment = "<UnitCode>" + UnitCode + "</UnitCode>" +
                                    "<ApartmentNumber>" + ApartmentNumber + "</ApartmentNumber>" +
                                    "<Bedrooms>" + Bedrooms + "</Bedrooms>" +
                                    "<Bathrooms>" + Bathrooms.ToString("F") + "</Bathrooms>" +
                                    "<MonthlyRate>" + MonthlyRate.ToString("F") + "</MonthlyRate>" +
                                    "<SecurityDeposit>" + SecurityDeposit.ToString("F") + "</SecurityDeposit>" +
                                    "<OccupancyStatus>" + OccupancyStatus + "</OccupancyStatus>";
    
            elmXML.InnerXml = strNewApartment;
            docApartment.DocumentElement.AppendChild(elmXML);
    
            docApartment.Save(strFileName);
            ShowApartments();
        }
    }
  7. Return to the Payment form and double-click the Close button
  8. Implement its Click event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  9. Save all

Adding an Element With Value

Consider the following XML file named videos.xml:

<?xml version="1.0" encoding="utf-8" ?>
<videos>
  <title>The Distinguished Gentleman</title>
</videos>

If you want the element to have a value, the XmlDocument class provides the CreateTextNode() method. This method returns an XmlText value. The syntax of this method is:

public virtual XmlText CreateTextNode(string text);

This method takes as argument the string that would constitute the value of the element. Before calling it, you should have used the XmlNode.AppendChild() method to create a node. Calling this method on the LastChild node of the one that called the AppendChild() would specify the value of the new node. Here is an example:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmRoot = DOMVideos.DocumentElement;

        XmlElement elmNew = DOMVideos.CreateElement("title");
        XmlText txtvideo = DOMVideos.CreateTextNode("Basic Instinct");
        elmRoot.AppendChild(elmNew);
        elmRoot.LastChild.AppendChild(txtvideo);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <title>The Distinguished Gentleman</title>
  <title>Basic Instinct</title>
</videos>

The combination of calls to XmlDocument.CreateElement() and XmlDocument.CreateTextNode() allow you to create a new element that has a value.

Consider the following XML file named "videos.xml":

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
  </video>
  <video>
    <title>Basic Instinct</title>
  </video>
</videos>

Notice that the root, videos, has a repetitive child named video. This video child has its own child named Title. Imagine that you want to add a new video node that has a child. To do this, first create an empty video node as a child of the root. We learned earlier how to do that:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmRoot = DOMVideos.DocumentElement;
        XmlElement elmNew = DOMVideos.CreateElement("video");
        elmRoot.AppendChild(elmNew);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

After creating the new child of the root, initialize the grand child with the desired name (the name doesn't have to be one of the existing names) and a value (which is optional if you don't want the new node to have a value). Once the new node is ready, append it as the last child of the root. If this new node has a value, append its XmlText object as the LastChild of the LastChild of the root. Here is an example of how you would do this:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmRoot = DOMVideos.DocumentElement;
        XmlElement elmNew = DOMVideos.CreateElement("video");
        elmRoot.AppendChild(elmNew);

        elmRoot = DOMVideos.DocumentElement;

        elmNew = DOMVideos.CreateElement("title");
        XmlText txtvideo = DOMVideos.CreateTextNode("Her Alibi");

        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
  </video>
  <video>
    <title>Basic Instinct</title>
  </video>
  <video>
    <title>Her Alibi</title>
  </video>
</videos>

Now consider the following file:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
</videos>

The root, videos, has a child named video. The video node has many child nodes. By now, we know how to add a child to the root. We also saw how to add a grand child with value to the root. To had many (grand) children to a node, first build the node, add it to the root, then continuously add the necessary nodes, one at a time, including its name and its optional value. This would be done as follows:

private void btnDocument_Click(object sender, EventArgs e)
{
    string Filename = "videos.xml";
    XmlDocument DOMVideos = new XmlDocument();

    if (File.Exists(Filename))
    {
        DOMVideos.Load(Filename);

        XmlElement elmRoot = DOMVideos.DocumentElement;
        XmlElement elmNew = DOMVideos.CreateElement("video");
        elmRoot.AppendChild(elmNew);

        elmRoot = DOMVideos.DocumentElement;

        elmNew = DOMVideos.CreateElement("title");
        XmlText txtvideo = DOMVideos.CreateTextNode("The Day After Tomorrow");
        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);

        elmNew = DOMVideos.CreateElement("director");
        txtvideo = DOMVideos.CreateTextNode("Roland Emmerich");
        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);

        elmNew = DOMVideos.CreateElement("length");
        txtvideo = DOMVideos.CreateTextNode("124 Minutes");
        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);

        elmNew = DOMVideos.CreateElement("format");
        txtvideo = DOMVideos.CreateTextNode("DVD");
        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);

        elmNew = DOMVideos.CreateElement("rating");
        txtvideo = DOMVideos.CreateTextNode("PG-13");
        elmRoot.LastChild.AppendChild(elmNew);
        elmRoot.LastChild.LastChild.AppendChild(txtvideo);
    }
    else // if (!File.Exists(Filename))
    {
        // Create the default XML structure
        DOMVideos.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                          "<videos></videos>");
    }
            
    DOMVideos.Save(Filename);
}

 This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
  <video>
    <title>The Day After Tomorrow</title>
    <director>Roland Emmerich</director>
    <length>124 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
  </video>
</videos>

Using the same approach, you can add children to children of children, and so on.

Practical LearningPractical Learning: Programmatically Adding an Element

  1. Display the Tenant Registration Editor dialog box and double-click the Submit button
  2. Implement the event as follows:
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        ushort NumberOfChildren;
        DateTime RegistrationDate, RentStartDate;
        string RegistrationNumber, EmployeeNumber, TenantCode, FirstName, 
               LastName, MaritalStatus, PhoneNumber, EmailAddress, UnitCode;
    
        XmlDocument xdRegistration = new XmlDocument();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\TenantsRegistrations.xml";
    
        if (!File.Exists(strFileName))
        {
            xdRegistration.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                   "<TenantsRegistrations></TenantsRegistrations>");
            xdRegistration.Save(strFileName);
        }
        
        xdRegistration.Load(strFileName);
        
        RegistrationNumber = txtRegistrationNumber.Text;
        RegistrationDate = dtpRegistrationDate.Value;
        EmployeeNumber = txtEmployeeNumber.Text;
        TenantCode = txtTenantCode.Text;
        FirstName = txtFirstName.Text;
        LastName = txtLastName.Text;
        MaritalStatus = cbxMaritalsStatus.Text;
        NumberOfChildren = ushort.Parse(txtNumberOfChildren.Text);
        PhoneNumber = txtPhoneNumber.Text;
        EmailAddress = txtEmailAddress.Text;
        UnitCode = txtUnitCode.Text;
        RentStartDate = dtpRentStartDate.Value;
        
        XmlElement elmRegistration = xdRegistration.CreateElement("TenantRegistration");
        xdRegistration.DocumentElement.AppendChild(elmRegistration);
            
        elmRegistration = xdRegistration.CreateElement("RegistrationNumber");
        XmlText txtRegistration = xdRegistration.CreateTextNode(RegistrationNumber);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("RegistrationDate");
        txtRegistration = xdRegistration.CreateTextNode(RegistrationDate.ToString("d"));
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("EmployeeNumber");
        txtRegistration = xdRegistration.CreateTextNode(EmployeeNumber);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("TenantCode");
        txtRegistration = xdRegistration.CreateTextNode(TenantCode);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("FirstName");
        txtRegistration = xdRegistration.CreateTextNode(FirstName);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("LastName");
        txtRegistration = xdRegistration.CreateTextNode(LastName);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("MaritalStatus");
        txtRegistration = xdRegistration.CreateTextNode(MaritalStatus);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
        
        elmRegistration = xdRegistration.CreateElement("NumberOfChildren");
        txtRegistration =        xdRegistration.CreateTextNode(NumberOfChildren.ToString());
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
        
        elmRegistration = xdRegistration.CreateElement("PhoneNumber");
        txtRegistration = xdRegistration.CreateTextNode(PhoneNumber);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
        
        elmRegistration = xdRegistration.CreateElement("EmailAddress");
        txtRegistration = xdRegistration.CreateTextNode(EmailAddress);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        elmRegistration = xdRegistration.CreateElement("UnitCode");
        txtRegistration = xdRegistration.CreateTextNode(UnitCode);
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
        
        elmRegistration = xdRegistration.CreateElement("RentStartDate");
        txtRegistration =        xdRegistration.CreateTextNode(RentStartDate.ToString("d"));
        xdRegistration.DocumentElement.LastChild.AppendChild(elmRegistration);
        xdRegistration.DocumentElement.LastChild.LastChild.AppendChild(txtRegistration);
    
        xdRegistration.Save(strFileName);
        Close();
    }
  3. Return to the Rental Allocation form and double-click the Close button
  4. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  5. Display the Payment Editor dialog box and double-click the Submit button
  6. Implement the event as follows:
    private void btnSubmit_Click(object sender, EventArgs e)
    {
        XmlDocument xdPayments = new XmlDocument();
        string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Payments.xml";
    
        // If some payments were previously made
        if( !File.Exists(strFileName) )
        {
            // If no payment has ever been made,
            // create a new XML file for the payments
            xdPayments.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                               "<Payments></Payments>");
            xdPayments.Save(strFileName);
        }
            
        // Open the payments.xml file
        xdPayments.Load(strFileName);
    
        // Locate the root element
        XmlElement xeDocumentElement = xdPayments.DocumentElement;
        // Get a list of the child nodes
        XmlNodeList xnlPayments = xeDocumentElement.ChildNodes;
    
        // Get a reference to the root element
        XmlElement xeRoot = xdPayments.DocumentElement;
        // Create an element named Payment
        XmlElement xePayment = xdPayments.CreateElement("Payment");
        // Add the new element as the last node of the root element
        xeRoot.AppendChild(xePayment);
        
        // Get a reference to the root element
        xeRoot = xdPayments.DocumentElement;
    
        // Create an element
        xePayment = xdPayments.CreateElement("ReceiptNumber");
        // Create the value of the new element
        XmlText txtPayment = xdPayments.CreateTextNode(txtReceiptNumber.Text);
        // Add the new element as a child of the Payment element
        xeRoot.LastChild.AppendChild(xePayment);
        // Specify the value of the new element
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
    
        // Follow the same logic for the other elements
        
        xePayment = xdPayments.CreateElement("PaymentDate");
        txtPayment = xdPayments.CreateTextNode(dtpPaymentDate.Value.ToShortDateString());
        xeRoot.LastChild.AppendChild(xePayment);
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
    
        xePayment = xdPayments.CreateElement("EmployeeNumber");
        txtPayment = xdPayments.CreateTextNode(txtEmployeeNumber.Text);
        xeRoot.LastChild.AppendChild(xePayment);
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
        
        xePayment = xdPayments.CreateElement("RegistrationNumber");
        txtPayment = xdPayments.CreateTextNode(txtRegistrationNumber.Text);
        xeRoot.LastChild.AppendChild(xePayment);
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
        
        xePayment = xdPayments.CreateElement("PaymentAmount");
        txtPayment = xdPayments.CreateTextNode(txtPaymentAmount.Text);
        xeRoot.LastChild.AppendChild(xePayment);
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
        
        xePayment = xdPayments.CreateElement("Notes");
        txtPayment = xdPayments.CreateTextNode(txtNotes.Text);
        xeRoot.LastChild.AppendChild(xePayment);
        xeRoot.LastChild.LastChild.AppendChild(txtPayment);
        
        xdPayments.Save(strFileName);
        Close();
    /*    XmlDocument xdPayment = new XmlDocument();
    
        string strFileName = @"C:\Microsoft Visual C# Application Design\Lambda Square Apartments\Payments.xml";
                
        if (!File.Exists(strFileName))
        {
            xdPayment.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                "<Payments></Payments>");
            xdPayment.Save(strFileName);
        }
                
        xdPayment.Load(strFileName);
                
        XmlElement nodRoot = xdPayment.DocumentElement;
                
        XmlElement elmXML = xdPayment.CreateElement("Payment");
                
        string strNewPayment = "<ReceiptNumber>" + int.Parse(txtReceiptNumber.Text) + "</ReceiptNumber>" +
                                "<PaymentDate>" + dtpPaymentDate.Value + "</PaymentDate>" +
                                "<EmployeeNumber>" + txtEmployeeNumber.Text + "</EmployeeNumber>" +
                                "<RegistrationNumber>" + int.Parse(txtRegistrationNumber.Text) + "</RegistrationNumber>" +
                                "<PaymentAmount>" + double.Parse(txtPaymentAmount.Text) + "</PaymentAmount>" +
                                "<Notes>" + txtNotes.Text + "</Notes>";
        elmXML.InnerXml = strNewPayment;
        xdPayment.DocumentElement.AppendChild(elmXML);
                
        xdPayment.Save(strFileName);
        Close();*/
    }
  7. Return to the Rental Allocation form and double-click the Close button
  8. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  9. Execute the application
  10. Create the aparments as follows:
    Unit Code Aprt # Bedrooms Bathrooms Monthly Rate Security Deposit Occupancy Status
    399475 101 2 2 1150 650 Available
    508293 102 1 1 950 500 Needs Repair
    729397 103 1 1 925 500 Available
    928364 104 3 2 1350 850 Available
    297297 105 2 1 1150 550 Available
    492739 106 3 2 1350 850 Available
    692797 107 3 2 1285 850 Not Ready
    829475 108 1 1 885 500 Available
    139749 109 2 2 1150 650 Available
    369294 110 1 1 895 500 Available
    502084 111 2 2 1145 650 Available
    829397 112 2 1 1085 600 Available
    292739 201 2 1 1185 650 Available
    496055 202 1 1 895 500 Available
    939595 203 1 1 925 500 Available
    384068 204 3 2 1250 850 Available
    824850 205 2 1 1100 600 Available
    620485 206 3 2 1300 850 Available
    294940 207 3 2 1350 850 Available
    602048 208 1 1 920 500 Available
    829479 209 2 2 1150 650 Available
    280484 210 1 1 895 500 Available
    602408 211 2 2 1175 650 Available
    384086 212 2 1 1075 600 Available
    397493 301 2 2 1175 650 Available
    625941 302 1 1 950 500 Available
    404950 303 1 1 925 500 Available
    304806 304 3 2 1250 850 Available
    844850 305 2 1 1100 600 Needs Repair
    596305 306 3 2 1300 850 Available
    138408 307 3 2 1350 850 Available
    305860 308 1 1 920 500 Available
    847584 309 2 2 1150 650 Available
    746959 310 1 1 935 500 Available
    359405 311 2 2 1175 650 Available
    308505 312 2 1 1075 600 Available

  11. Create employees records as follows:
     
    Employee # First Name Last Name Title
    93947 Leonard Goulet Owner - General Manager
    40685 Justine Sandt Rent Manager
    73048 Raymond Wilkinson  
    60949 Mark Reason Maintenance Technician
    38408 Marc Knights Rent Associate
    20448 Nancy Longhorn Rent Associate
  12. Create the registrations records as follows:
     
    Regist # Registration Date Processed By Tenant Code First Name Last Name Marital Status Children Phone Number Email Address Tenancy Status Unit Code Rent Start Date
    1001 6/12/2014 38408 29-485-05 Ann Sanders Married 1 (240) 524 -2831 annsanders@emailcity.com Active 139749 7/1/2014
    1002 6/16/2014 20448 83-400-85 Mahty Shaoul Married 2 202-729-1574 mshaoulman@gmail.com Active 928364 9/1/2014
    1003 6/24/2014 40685 48-602-73 Frank Ulm Single 0 (301) 882-0704 fulm112244@yaho.com Active 729397 7/1/2014
    1004 6/24/2014 93947 24-385-30 Elise Provoski Separated 1 (443) 974-9631 eprevalence@yahoo.com Active 844850 8/1/2014
    1005 7/23/2014 93947 92-048-11 Grace Curryan Married 1 (240) 927-0993 gcarrier@gmail.com Active 297297 9/1/2014
    1006 7/25/2014 38408 51-304-58 Tracy Warrens Divorced 2 202-793-6924 twarrior12@hotmail.coom Active 492739 8/1/2014
    1007 8/1/2014 38408 72-384-04 Paul Yamo Married 3 (410-792-7045 pyamo@hr.umd.edu Active 384068 10/1/2014
    1008 8/10/2014 40685 62-405-29 Nancy Shermann Single 1 (703) 338-2973 nsherre@emailcity.com Active 829475 9/1/2014
    1009 9/12/2014 20448 72-484-04 Michael Tiernan Single 0 301-274-9285 resdev.globelan.net Active 829479 11/1/2014
    1010 10/5/2014 38408 60-285-83 Phillippe Anderson Single 0 202-729-1574 philanders@gmail.com Active 496055 11/1/2014
  13. Create payments records as follows:
     
    Receipt # Payment Date Empl # Regist # Amount Notes
    100001 6/12/2014 38408 1001 650 This is the payment for the security deposit.
    100002 6/20/2014 40685 1003 500 Security Deposit
    100003 7/27/2014 38408 1003 925  
    100004 7/28/2014 38408 1001 1150  
    100005 8/1/2014 40685 1006 850 Security Deposit
    100006 8/8/2014 40685 1007 850 Security Deposit
    100007 8/8/2014 40685 1008 500 Security Deposit
    100008 8/13/2014 40685 1004 600 Security Deposit
    100009 8/14/2014 20448 1002 850 Payment for security deposit
    100010 8/25/2014 38408 1001 1150  
    100011 8/25/2014 38408 1002 1350  
    100012 8/26/2014 20448 1003 925  
    100013 8/27/2014 40685 1004 1100  
    100014 8/30/2014 38408 1006 1350  
    100015 9/17/2014 40685 1009 650 Security Deposit
    100016 9/18/2014 20448 1005 550 Security Deposit
    100017 9/25/2014 20448 1004 1100  
    100018 9/25/2014 20448 1006 1350  
    100019 9/25/2014 20448 1008 885  
    100020 9/28/2014 20448 1001 1150  
    100021 9/28/2014 40685 1002 1350  
    100022 9/28/2014 40685 1005 1150  
    100023 10/5/2014 38408 1003 925  
    100024 10/8/2014 40685 1010 500 Security Deposit
    100025 10/24/2014 38408 1004 1100  
    100026 10/24/2014 38408 1005 1150  
    100027 10/25/2014 40685 1006 1350  
    100028 10/25/2014 40685 1007 1250  
    100029 10/27/2014 93947 1001 1150  
    100030 10/29/2014 93947 1008 885  
    100031 10/30/2014 38408 1002 1350  
    100032 10/31/2014 40685 1003 925  
    100033 11/26/2014 38408 1002 1350  
    100034 11/26/2014 38408 1008 885  
    100035 11/27/2014 38408 1006 1350  
    100036 11/28/2014 20448 1004 1100  
    100037 11/28/2014 38408 1005 1150  
    100038 11/28/2014 38408 1007 1250  
    100039 11/29/2014 93947 1001 1150  
    100040 11/30/2014 38408 1003 925  
    100041 11/30/2014 20448 1009 1150  
    100042 11/30/2014 20448 1010 895  
    100043 12/25/2014 38408 1006 1350  
    100044 12/25/2014 38408 1007 1250  
    100045 12/27/2014 20448 1009 1150  
    100046 12/28/2014 20448 1001 1150  
    100047 12/28/2014 38408 1004 1100  
    100048 12/28/2014 38408 1005 1150  
    100049 12/28/2014 38408 1010 895  
    100050 12/30/2014 20448 1003 925  
    100051 12/31/2014 38408 1002 1350  
    100052 12/31/2014 20448 1008 885  
    100053 1/23/2015 20448 1005 1150  
    100054 1/26/2015 38408 1001 1150  
    100055 1/28/2015 93947 1003 925  
    100056 1/29/2015 93947 1002 1350  
    100057 2/10/2015 20448 1004 100 This is a fee for late payment.
    100058 2/10/2015 20448 1004 1100  
    100059 2/20/2015 20448 1001 1150  
    100060 2/25/2015 20448 1005 1150  
    100061 2/26/2015 38408 1002 1350  
    100062 3/1/2015 38408 1004 1100  
    100063 3/3/2015 40685 1003 925  
  14. Close the forms and return to your programming environment

Application


Previous Copyright © 2005-2020, FunctionX Next