Home

XML-Based Application: Altair Realtors

     

Introduction

This application is used to review such XML characteristics as the attributes of an XML element.

 

Practical Learning: Introducing XML Attributes

  1. Start Microsoft Visual Studio and create a Windows Application named AltairRealtors1
  2. To create a new form, on the main menu, click Projects -> Add Windows Form...
  3. Set the Name to PropertyEditor and click Add
  4. Design the form as follows:
     
    Altair Realtors: Property Editor
    Control Text Name Other Properties
    Label Label Property Code:    
    TextBox TextBox   txtPropertyCode Modifiers: Public
    Label Label Status    
    ComboBox ComboBox   cbxStatus Modifiers: Public
    Items:
    Sold
    Available
    Needs Repair
    Label Label      
    DateTimePicker   dtpDateListed Modifiers: Public
    Format: Short
    Label Label Year Built:    
    TextBox TextBox   txtYearBuilt  
    Label Label Property Type:    
    ComboBox ComboBox   cbxPropertyTypes Modifiers: Public
    Items:
    Unknown
    Single Family
    Townhouse
    Condominium
    Label Label Style:    
    ComboBox ComboBox   cbxStyles Modifiers: Public
    Items:
    Farm
    Colonial
    Victorian
    Contemporary
    Label Label Address:    
    TextBox TextBox   txtAddress Modifiers: Public
    Label Label City:    
    TextBox TextBox   txtCity Modifiers: Public
    Label Label Location:    
    TextBox TextBox   txtLocation Modifiers: Public
    Label Label State:    
    ComboBox ComboBox   cbxStates Modifiers: Public
    Items:
    DC
    MD
    PA
    VA
    WV
    Label Label ZIP Code:    
    TextBox TextBox   txtZIPCode Modifiers: Public
    Label Label Stories    
    TextBox TextBox 0 txtStories Modifiers: Public
    Label Label Bedrooms:    
    TextBox TextBox 0 txtBedrooms Modifiers: Public
    Label Label Bathrooms:    
    TextBox TextBox 0.0 txtBathrooms Modifiers: Public
    Label Label Condition:    
    ComboBox ComboBox   cbxConditions Modifiers: Public
    Items:
    Good
    Excellent
    Needs Repairs
    Label Label Market Value:    
    TextBox TextBox 0.00 txtMarketValue Modifiers: Public
    Label Label Picture Path:    
    TextBox TextBox   txtPicturePath Modifiers: Public
    Enabled: False
    Button Button Select Picture... btnPicture  
    PictureBox   pbxProperty Modifiers: Public
    SizeMode: Zoom
    Button Button OK btnOK DialogResult: OK
    Button Button Cancel btnCancel DialogResult: Cancel
    OpenFileDialog (Name): dlgPicture
    Title: Select Property Picture
    DefaultExt: jpg
    Filter: JPEG Files (*.jpg,*.jpeg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp|PNG Files (*.png)|*.png
    Form
    FormBorderStyle: FixedDialog
    Text: Altair Realtors - Property Editor
    StartPosition: CenterScreen
    AcceptButton: btnOK
    CancelButton: btnCancel
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskBar: False
  5. Double-click the Select Picture button and implement its event as follows:
    private void btnPicture_Click(object sender, EventArgs e)
    {
        if (dlgPicture.ShowDialog() == DialogResult.OK)
        {
            txtPicturePath.Text = dlgPicture.FileName;
            pbxProperty.Image = Image.FromFile(txtPicturePath.Text);
        }
    }
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type AltairRealtor.cs and press Enter twice (to display that form)
  8. From the Toolbox, add a ListView to the form
  9. 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
    colIndex #   40
    colPropertyCode Prop Code Center 65
    colPropertyType Property Type   80
    colPropertyCondition Condition   65
    colLocation Location   70
    colStories Stories Right 45
    colBedrooms Bedrooms Right 62
    colBathrooms Bathrooms Right 62
    colMarketValue Market Value Right 75
  10. Design the form as follows: 
     
     
    Control Text Name Other Properties
    ListView   lvwAllocations View: Details
    GridLines: True
    FullRowSelect: True
    Anchor: Top, Bottom, Left, Right
    PictureBox   pbxProperty Anchor: Bottom, Left
    Label Description   Anchor: Bottom, Right
    TextBox   txtDescription Multiline: True
    Anchor: Bottom, Right
    Button New Property... btnNewProperty Anchor: Bottom, Right
    Button Close btnClose Anchor: Bottom, Right
  11. Based on what we learned in previous lessons, double-click the New Property button and implement its Click 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 AltairRealtors5a
    {
        public partial class AltairRealtor : Form
        {
            public AltairRealtor()
            {
                InitializeComponent();
            }
            
            private void btnNewProperty_Click(object sender, EventArgs e)
            {
                // Get a reference to the property editor
                PropertyEditor editor = new PropertyEditor();
                string strFilename = "properties.xml";
        
                if (editor.ShowDialog() == DialogResult.OK)
                {
                    string PropertyType, Location;
                    int Stories, Bedrooms;
                    float Bathrooms;
                    double MarketValue;
    
                    // We will need a reference to the XML document
                    XmlDocument docProperties = new XmlDocument();
    
                    // Find out if the file exists already
                    // If it doesn't, then create it
                    if (!File.Exists(strFilename))
                    {
                docProperties.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                      "<Listing></Listing>");
                        docProperties.Save(strFilename);
                    }
            
                    // Open the XML file
                    docProperties.Load(strFilename);
    
                    // Get a reference to the root node
                    XmlElement nodRoot = docProperties.DocumentElement;
    
                    PropertyType = editor.cbxPropertyTypes.Text;
                    Location = editor.txtLocation.Text;
                    Stories = int.Parse(editor.txtStories.Text);
                    Bedrooms = int.Parse(editor.txtBedrooms.Text);
                    Bathrooms = float.Parse(editor.txtBathrooms.Text);
                    MarketValue = double.Parse(editor.txtMarketValue.Text);
    
                    // Get a reference to the root element
                    XmlElement elmRoot = docProperties.DocumentElement;
                    // Create a node named Property
                    XmlElement elmProperty = docProperties.CreateElement("Property");
                    // Add it to the root element
                    elmRoot.AppendChild(elmProperty);
    
                    // Create a node named PropertyType
                    elmProperty = docProperties.CreateElement("PropertyType");
                    XmlText txtProperty = docProperties.CreateTextNode(PropertyType);
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Create a node named Location
                    elmProperty = docProperties.CreateElement("Location");
                    txtProperty = docProperties.CreateTextNode(Location);
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Create a node named Stories
                    elmProperty = docProperties.CreateElement("Stories");
                    txtProperty = docProperties.CreateTextNode(Stories.ToString());
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Create a node named Bedrooms
                    elmProperty = docProperties.CreateElement("Bedrooms");
                    txtProperty = docProperties.CreateTextNode(Bedrooms.ToString());
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Create a node named Bathrooms
                    elmProperty = docProperties.CreateElement("Bathrooms");
                txtProperty = docProperties.CreateTextNode(Bathrooms.ToString("F"));
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Create a node named MarketValue
                    elmProperty = docProperties.CreateElement("MarketValue");
                txtProperty = docProperties.CreateTextNode(MarketValue.ToString("F"));
                    elmRoot.LastChild.AppendChild(elmProperty);
                    elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                    // Save the XML file
                    docProperties.Save(strFilename);
                }
            }
        }
    }
  12. Execute the application
  13. Click the New Property button
  14. Enter the following pieces of information:
     
    Property Type Single Family
    Location White Oak
    Stories 2
    Bedrooms 3
    Bathrooms 2.5
    Market Value 365580
  15. Click OK
  16. Close the form and return to your programming environment
  17. From the main menu of Visual Studio, open the properties.xml file from the AltairRealtors5\AltairRealtors\bin\Debug folder: 
    <?xml version="1.0" encoding="utf-8"?>
    <Listing>
      <Property>
        <PropertyType>Single Family</PropertyType>
        <Location>White Oak</Location>
        <Stories>2</Stories>
        <Bedrooms>3</Bedrooms>
        <Bathrooms>2.50</Bathrooms>
        <MarketValue>365580.00</MarketValue>
      </Property>
    </Listing>
  18. Display the AltairRealtors.cs file

Practical Learning: Creating an Attribute

  1. To set attributes of a node, change the file as follows:
    private void btnNewProperty_Click(object sender, EventArgs e)
    {
        // Get a reference to the property editor
        PropertyEditor editor = new PropertyEditor();
                
        string strFilename = "properties.xml";
    
        Random rnd = new Random();
        editor.txtPropertyCode.Text = rnd.Next(100, 999).ToString() +
                                      "-" + rnd.Next(100, 999).ToString();
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            string PropertyCode, PropertyType, Location, SaleStatus;
            int Stories, Bedrooms;
            float Bathrooms;
            double MarketValue;
    
            // We will need a reference to the XML document
            XmlDocument docProperties = new XmlDocument();
    
            // Find out if the file exists already
            // If it doesn't, then create it
            if (!File.Exists(strFilename))
            {
                docProperties.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                      "<Listing></Listing>");
                docProperties.Save(strFilename);
            }
    
            // Open the XML file
            docProperties.Load(strFilename);
    
            // Get a reference to the root node
            XmlElement nodRoot = docProperties.DocumentElement;
    
            PropertyCode = editor.txtPropertyCode.Text;
            SaleStatus = editor.cbxStatus.Text;
            PropertyType = editor.cbxPropertyTypes.Text;
            Location = editor.txtLocation.Text;
            Stories = int.Parse(editor.txtStories.Text);
            Bedrooms = int.Parse(editor.txtBedrooms.Text);
            Bathrooms = float.Parse(editor.txtBathrooms.Text);
            MarketValue = double.Parse(editor.txtMarketValue.Text);
    
            // Get a reference to the root element
            XmlElement elmRoot = docProperties.DocumentElement;
            // Create a node named Property
            XmlElement elmProperty = docProperties.CreateElement("Property");
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Code", PropertyCode);
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Status", SaleStatus);
    
            // Add it to the root element
            elmRoot.AppendChild(elmProperty);
    
            // Create a node named PropertyType
            . . . No Change
        }
    }
  2. Execute the application
  3. Click the New Property button
  4. Enter the following pieces of information:
     
    Status Sold
    Property Type Single Family
    Location Arlington Cemetery
    Stories 3
    Bedrooms 5
    Bathrooms 3.5
    Market Value 675880
  5. Click OK
  6. Close the form and return to your programming environment
  7. In Visual Studio, access the properties.xml tab to see the file (the value of the Code attribute will be different from yours):
    <?xml version="1.0" encoding="utf-8"?>
    <Listing>
      <Property>
        <PropertyType>Single Family</PropertyType>
        <Location>White Oak</Location>
        <Stories>2</Stories>
        <Bedrooms>3</Bedrooms>
        <Bathrooms>2.50</Bathrooms>
        <MarketValue>365580.00</MarketValue>
      </Property>
      <Property Code="700-326" Status="Sold">
        <PropertyType>Single Family</PropertyType>
        <Location>Arlington Cemetery</Location>
        <Stories>3</Stories>
        <Bedrooms>5</Bedrooms>
        <Bathrooms>3.50</Bathrooms>
        <MarketValue>675880.00</MarketValue>
      </Property>
    </Listing>
  8. Display the AltairRealtors.cs file

Practical Learning: Adding an Attribute to an Element

  • To add a few attributes, change the file as follows:
    private void btnNewProperty_Click(object sender, EventArgs e)
    {
        // Get a reference to the property editor
        PropertyEditor editor = new PropertyEditor();
        // If this directory doesn't exist, create it
        Directory.CreateDirectory(@"C:\Altair Realtors");
        // This is the XML file that holds the list of proeprties
        string strFilename = @"C:\Altair Realtors\properties.xml";
    
        Random rnd = new Random();
        editor.txtPropertyCode.Text = rnd.Next(100, 999).ToString() +
                                      "-" + rnd.Next(100, 999).ToString();
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            string PropertyCode, PropertyType, Location,
                   SaleStatus, Style, Condition;
            int YearBuilt, Stories, Bedrooms;
            float Bathrooms;
            double MarketValue;
    
            // We will need a reference to the XML document
            XmlDocument docProperties = new XmlDocument();
    
            // Find out if the file exists already
            // If it doesn't, then create it
            if (!File.Exists(strFilename))
            {
                docProperties.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                      "<Listing></Listing>");
                docProperties.Save(strFilename);
            }
    
            // Open the XML file
            docProperties.Load(strFilename);
    
            // Get a reference to the root node
            XmlElement nodRoot = docProperties.DocumentElement;
    
            PropertyCode = editor.txtPropertyCode.Text;
            SaleStatus = editor.cbxStatus.Text;
            YearBuilt = int.Parse(editor.txtYearBuilt.Text);
            PropertyType = editor.cbxPropertyTypes.Text;
            Style = editor.cbxStyle.Text;
            Condition = editor.cbxConditions.Text;
            Bedrooms = int.Parse(editor.txtBedrooms.Text);
            Bathrooms = float.Parse(editor.txtBathrooms.Text);
            MarketValue = double.Parse(editor.txtMarketValue.Text);
    
            // Get a reference to the root element
            XmlElement elmRoot = docProperties.DocumentElement;
            // Create a node named Property
            XmlElement elmProperty = docProperties.CreateElement("Property");
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Code", PropertyCode);
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Status", SaleStatus);
            // Add it to the root element
            elmRoot.AppendChild(elmProperty);
    
            // Create a node named Property
            elmProperty = docProperties.CreateElement("DateListed");
            elmRoot.LastChild.AppendChild(elmProperty);
    
            // Create an attribute for the DateListed element
            XmlAttribute atrProperty = docProperties.CreateAttribute("YearBuilt");
            atrProperty.Value = YearBuilt.ToString();
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create a node named PropertyType
            elmProperty = docProperties.CreateElement("PropertyType");
            XmlText txtProperty = docProperties.CreateTextNode(PropertyType);
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create an attribute for the PropertyType element
            atrProperty = docProperties.CreateAttribute("Style");
            atrProperty.Value = Style;
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create an attribute for the PropertyType element
            atrProperty = docProperties.CreateAttribute("Condition");
            atrProperty.Value = Condition;
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create a node named Location
            elmProperty = docProperties.CreateElement("Location");
            txtProperty = docProperties.CreateTextNode(Location);
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            . . . No Change
    
            // Save the XML file
            docProperties.Save(strFilename);
        }
    }      

 

Practical Learning: Adding Attributes

  1. To add a few attributes, change the file as follows:
    private void btnNewProperty_Click(object sender, EventArgs e)
    {
        // Get a reference to the property editor
        PropertyEditor editor = new PropertyEditor();
        // If this directory doesn't exist, create it
        string strDirectory = @"C:\Altair Realtors";
        Directory.CreateDirectory(strDirectory);
        // This is the XML file that holds the list of proeprties
        string strFilename = @"C:\Altair Realtors\properties.xml";
    
        Random rnd = new Random();
        editor.txtPropertyCode.Text = rnd.Next(100, 999).ToString() +
                                      "-" + rnd.Next(100, 999).ToString();
        string strPicturePath;
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            string PropertyCode, PropertyType, Location,
            SaleStatus, Style, Condition,
            Address, City, State, ZIPCode, Description;
            DateTime DateListed;
            int YearBuilt, Stories, Bedrooms;
            float Bathrooms;
            double MarketValue;
    
            // We will need a reference to the XML document
            XmlDocument docProperties = new XmlDocument();
    
            // Find out if the file exists already
            // If it doesn't, then create it
            if (!File.Exists(strFilename))
            {
                docProperties.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                      "<Listing></Listing>");
                docProperties.Save(strFilename);
            }
    
            // Open the XML file
            docProperties.Load(strFilename);
    
            // Get a reference to the root node
            XmlElement nodRoot = docProperties.DocumentElement;
    
            PropertyCode = editor.txtPropertyCode.Text;
            SaleStatus = editor.cbxStatus.Text;
            DateListed = editor.dtpDateListed.Value;
            YearBuilt = int.Parse(editor.txtYearBuilt.Text);
            PropertyType = editor.cbxPropertyTypes.Text;
            Style = editor.cbxStyle.Text;
            Condition = editor.cbxConditions.Text;
            Location = editor.txtLocation.Text;
            Address = editor.txtAddress.Text;
            City = editor.txtCity.Text;
            State = editor.cbxStates.Text;
            ZIPCode = editor.txtZIPCode.Text;
            Stories = int.Parse(editor.txtStories.Text);
            Bedrooms = int.Parse(editor.txtBedrooms.Text);
            Bathrooms = float.Parse(editor.txtBathrooms.Text);
            Description = editor.txtDescription.Text;
            MarketValue = double.Parse(editor.txtMarketValue.Text);
            strPicturePath = editor.txtPicturePath.Text;
    
            // Get a reference to the root element
            XmlElement elmRoot = docProperties.DocumentElement;
            // Create a node named Property
            XmlElement elmProperty = docProperties.CreateElement("Property");
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Code", PropertyCode);
    
            // Create an attribute for the Propety node
            elmProperty.SetAttribute("Status", SaleStatus);
            // Add it to the root element
            elmRoot.AppendChild(elmProperty);
    
            // Create a node named Property
            elmProperty = docProperties.CreateElement("DateListed");
            XmlText txtProperty = docProperties.CreateTextNode(DateListed.ToString("d"));
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
            elmRoot.LastChild.AppendChild(elmProperty);
    
            // Create an attribute for the DateListed element
            XmlAttribute atrProperty = docProperties.CreateAttribute("YearBuilt");
            atrProperty.Value = YearBuilt.ToString();
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create a node named PropertyType
            elmProperty = docProperties.CreateElement("PropertyType");
            txtProperty = docProperties.CreateTextNode(PropertyType);
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create an attribute for the PropertyType element
            atrProperty = docProperties.CreateAttribute("Style");
            atrProperty.Value = Style;
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create an attribute for the PropertyType element
            atrProperty = docProperties.CreateAttribute("Condition");
            atrProperty.Value = Condition;
            elmProperty.SetAttributeNode(atrProperty);
    
            // Create a node named Location
            elmProperty = docProperties.CreateElement("Location");
            txtProperty = docProperties.CreateTextNode(Location);
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create some attributes for the Location element
            atrProperty = docProperties.CreateAttribute("Address");
            atrProperty.Value = Address;
            elmProperty.Attributes.Append(atrProperty);
                    
            atrProperty = docProperties.CreateAttribute("City");
            atrProperty.Value = City;
            elmProperty.Attributes.Append(atrProperty);
    
            atrProperty = docProperties.CreateAttribute("State");
            atrProperty.Value = State;
            elmProperty.Attributes.Append(atrProperty);
    
            atrProperty = docProperties.CreateAttribute("ZIPCode");
            atrProperty.Value = ZIPCode;
            elmProperty.Attributes.Append(atrProperty);
    
            // Create a node named Stories
            elmProperty = docProperties.CreateElement("Stories");
            txtProperty = docProperties.CreateTextNode(Stories.ToString());
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create a node named Bedrooms
            elmProperty = docProperties.CreateElement("Bedrooms");
            txtProperty = docProperties.CreateTextNode(Bedrooms.ToString());
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create a node named Bathrooms
            elmProperty = docProperties.CreateElement("Bathrooms");
            txtProperty = docProperties.CreateTextNode(Bathrooms.ToString("F"));
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create a node named MarketValue
            elmProperty = docProperties.CreateElement("MarketValue");
            txtProperty = docProperties.CreateTextNode(MarketValue.ToString("F"));
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Create a node named Description
            elmProperty = docProperties.CreateElement("Description");
            txtProperty = docProperties.CreateTextNode(Description);
            elmRoot.LastChild.AppendChild(elmProperty);
            elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
            // Save the XML file
            docProperties.Save(strFilename);
    
            if (strPicturePath.Length != 0)
            {
                // The following code gets a reference to the picture's name 
                FileInfo flePicture = new FileInfo(strPicturePath);
                // Then it copies it to the directory of this business
                // It changes its name to be the same as the property code
                flePicture.CopyTo(@"C:\Altair Realtors\" +
                                  PropertyCode +
                                  flePicture.Extension);
            }
    
            ShowListing();
    
            if( lvwProperties.Items.Count > 0 )
                lvwProperties.Items[0].Selected = true;
        }
    }
    
    void ShowListing()
    {
    }
  2. Execute the application and create a few properties
  3. Close the form and return to your programming environment

Practical Learning: Accessing Some Attributes

  1. Display the AltairRealtors.cs file and double-click an unoccupied area of its body
  2. Implement the event as follows:
    void ShowListing()
    {
        XmlDocument docProperties = new XmlDocument();
        string strFilename = @"C:\Altair Realtors\properties.xml";
    
        if (File.Exists(strFilename))
        {
            lvwProperties.Items.Clear();
    
            docProperties.Load(strFilename);
            XmlElement elmProperty = docProperties.DocumentElement;
            XmlNodeList lstProperties = elmProperty.ChildNodes;
    
            int i = 1;
    
            foreach (XmlNode node in lstProperties)
            {
               ListViewItem lviProperty = new ListViewItem(i.ToString());
    
               lviProperty.SubItems.Add(node.Attributes[0].InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.Attributes[1].InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
               lviProperty.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.NextSibling.InnerText);
               lvwProperties.Items.Add(lviProperty);
               i++;
           }
    
            txtDescription.Text = lstProperties[0]["Description"].InnerText;
        }
    }
    
    private void AltairRealtor_Load(object sender, EventArgs e)
    {
        ShowListing();
    
        if( lvwProperties.Items.Count > 0 )
            lvwProperties.Items[0].Selected = true;
    }
  3. Return to the form and double-click the list view
  4. Implement the event as follows:
    private void lvwProperties_SelectedIndexChanged(object sender, EventArgs e)
    {
        if( (lvwProperties.SelectedItems.Count == 0) ||
            (lvwProperties.SelectedItems.Count > 1) )
             return;
    
        string strPropertyCode = lvwProperties.SelectedItems[0].SubItems[1].Text;
    
        // Make a list of the picture files
        string strDirectory = @"C:\Altair Realtors";
        DirectoryInfo dirProperties = new DirectoryInfo(strDirectory);
        FileInfo[] PictureFiles = dirProperties.GetFiles();
    
        // Look for a file that holds the same name as the item number
        foreach (FileInfo fle in PictureFiles)
        {
            // Get the name of the file without its extension
            string fwe = Path.GetFileNameWithoutExtension(fle.FullName);
    
            if( fwe == strPropertyCode )
                pbxProperty.Image = Image.FromFile(strDirectory +
                      "\\" + strPropertyCode + fle.Extension);
        }
    
        XmlDocument docProperties = new XmlDocument();
        string strFilename = @"C:\Altair Realtors\properties.xml";
    
        if( File.Exists(strFilename) )
        {
            docProperties.Load(strFilename);
            XmlElement elmProperty = docProperties.DocumentElement;
            XmlNodeList lstProperties = elmProperty.ChildNodes;
    
            foreach(XmlNode node in lstProperties)
            {
                if( node.Attributes[0].InnerText == strPropertyCode )
                    txtDescription.Text = node["Description"].InnerText;
            }
        }
    }
  5. Return to the form and double-click the Close button
  6. Implement the event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  7. Execute the application

Practical Learning: Creating Comments

  1. Access the AltairRealtors.cs file and change it 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 AltairRealtors5a
    {
        public partial class AltairRealtor : Form
        {
            public AltairRealtor()
            {
                InitializeComponent();
            }
    
            private void btnNewProperty_Click(object sender, EventArgs e)
            {
                // Get a reference to the property editor
                PropertyEditor editor = new PropertyEditor();
                // If this directory doesn't exist, create it
                string strDirectory = @"C:\Altair Realtors";
                Directory.CreateDirectory(strDirectory);
                // This is the XML file that holds the list of proeprties
                string strFilename = @"C:\Altair Realtors\properties.xml";
    
                Random rnd = new Random();
                editor.txtPropertyCode.Text = rnd.Next(100, 999).ToString() +
                                              "-" + rnd.Next(100, 999).ToString();
                string strPicturePath;
    
                if (editor.ShowDialog() == DialogResult.OK)
                {
                    string PropertyCode, PropertyType, Location,
                    SaleStatus, Style, Condition,
                    Address, City, State, ZIPCode, Description;
                    DateTime DateListed;
                    int YearBuilt, Stories, Bedrooms;
                    float Bathrooms;
                    double MarketValue;
    
                    // Create the values of the nodes and attributes
                    PropertyCode = editor.txtPropertyCode.Text;
                    SaleStatus = editor.cbxStatus.Text;
                    DateListed = editor.dtpDateListed.Value;
                    YearBuilt = int.Parse(editor.txtYearBuilt.Text);
                    PropertyType = editor.cbxPropertyTypes.Text;
                    Style = editor.cbxStyle.Text;
                    Condition = editor.cbxConditions.Text;
                    Location = editor.txtLocation.Text;
                    Address = editor.txtAddress.Text;
                    City = editor.txtCity.Text;
                    State = editor.cbxStates.Text;
                    ZIPCode = editor.txtZIPCode.Text;
                    Stories = int.Parse(editor.txtStories.Text);
                    Bedrooms = int.Parse(editor.txtBedrooms.Text);
                    Bathrooms = float.Parse(editor.txtBathrooms.Text);
                    Description = editor.txtDescription.Text;
                    MarketValue = double.Parse(editor.txtMarketValue.Text);
                    strPicturePath = editor.txtPicturePath.Text;
    
                    // We will need a reference to the XML document
                    XmlDocument docProperties = new XmlDocument();
                    XmlElement elmRoot;
    
                    // Find out if the file exists already
                    // If it doesn't, then create it
                    if (!File.Exists(strFilename))
                    {
                        docProperties.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                              "<Listing></Listing>");
    
                        // Get a reference to the root element
                        elmRoot = docProperties.DocumentElement;
    
                        // Add a comment to it
                        string strComment = "This is code for a real estate business.\n";
                        XmlComment cmtDescription = docProperties.CreateComment(strComment);
                        docProperties.InsertBefore(cmtDescription, elmRoot);
    
                        // Get a reference to the root element
                        elmRoot = docProperties.DocumentElement;
    
                        strComment = "A property is created with the Property element.\n";
                        strComment += "\tFor each property, a random number is created.\n";
                        strComment += "\tThis number is made of two parts that each\n";
                        strComment += "\tranges from 100 to 999.\n";
                        strComment += "\tBoth parts are separated by a dash.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.AppendChild(cmtDescription);
    
                        strComment = "The sale status, named Status, indicates whether\n";
                        strComment += "\tthe property is currently available or has\n";
                        strComment += "\talready been sold.\n";
                        strComment += "\tThe possible values are Available and Sold.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.AppendChild(cmtDescription);
    
                        // Create a node named Property
                        XmlElement elmProperty = docProperties.CreateElement("Property");
    
                        // Create an attribute for the Propety node
                        elmProperty.SetAttribute("Code", PropertyCode);
    
                        // Create an attribute for the Propety node
                        elmProperty.SetAttribute("Status", SaleStatus);
                        // Add it to the root element
                        elmRoot.AppendChild(elmProperty);
    
                        strComment = "An element named DateListed specifies when\n";
                        strComment += "\tthe property was added to the listing\n";
                        strComment += "\tor when it entered in the market.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        strComment = "As its name suggests, the YearBuilt attribute\n";
                        strComment += "\tindicates the year the property was built.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Property
                        elmProperty = docProperties.CreateElement("DateListed");
                        XmlText txtProperty = docProperties.CreateTextNode(DateListed.ToString("d"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create an attribute for the DateListed element
                        XmlAttribute atrProperty = docProperties.CreateAttribute("YearBuilt");
                        atrProperty.Value = YearBuilt.ToString();
                        elmProperty.SetAttributeNode(atrProperty);
    
                        strComment = "The property type must be\n";
                        strComment += "\t* Single Family: For a house that stands by itself\n";
                        strComment += "\t* Townhouse: For a full house but that is attached\n";
                        strComment += "\t\tto at least another house.\n";
                        strComment += "\t* Condominium: For a type of apartment that\n";
                        strComment += "\t\tbelongs to a building.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named PropertyType
                        elmProperty = docProperties.CreateElement("PropertyType");
                        txtProperty = docProperties.CreateTextNode(PropertyType);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        strComment = "The style of a property indicates a type of ";
                        strComment += "\tarchitecture.\n";
                        strComment += "\tThe possible styles are Contemporary, Colonial,\n";
                        strComment += "\tVictorian, or Farmhouse.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create an attribute for the PropertyType element
                        atrProperty = docProperties.CreateAttribute("Style");
                        atrProperty.Value = Style;
                        elmProperty.SetAttributeNode(atrProperty);
    
                        strComment = "The Condition attribute specifies whether the\n";
                        strComment += "\tproperty can currently be presented to a customer,\n";
                        strComment += "\tor at least how it appears at the moment.\n";
                        strComment += "\tThe possible conditions are:\n";
                        strComment += "\t* Excellent: This suggests that the property has\n";
                        strComment += "\t\tlittle to no flaw\n";
                        strComment += "\t* Good: This means that the property is mostly\n";
                        strComment += "\t\tacceptable and can be presented to a potential buyer.\n";
                        strComment += "\t\tIn this case, something such as a refrigerator or\n";
                        strComment += "\t\ta dishwasher may be missing; or something such as\n";
                        strComment += "\t\tthe garbage dispenser may need to be fixed, or a\n";
                        strComment += "\t\tpiece of carpet that needs to be cleaned before the\n";
                        strComment += "\t\tproperty is actually ready for sale\n";
                        strComment += "\t* Needs Repair: This means that the property is\n";
                        strComment += "\t\tin a good selling condition.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create an attribute for the PropertyType element
                        atrProperty = docProperties.CreateAttribute("Condition");
                        atrProperty.Value = Condition;
                        elmProperty.SetAttributeNode(atrProperty);
    
                        strComment = "A property can be located by its address which\n";
                        strComment += "\tin most cases is made of an address, a city,\n";
                        strComment += "\ta state (US, DE, AU, NG, etc) or\n";
                        strComment += "\tprovince (CA, CM, etc), a ZIP Code (US) or\n";
                        strComment += "\tpostal code (CA, GB, BE, etc).";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        strComment = "To make it easy to locally find a property,\n";
                        strComment += "\tthe location may not be an actual\n";
                        strComment += "\tcity and usually people out of town may have a\n";
                        strComment += "\thard time finding it on a map. Nevertheless,\n";
                        strComment += "\tlocal people, the inhabitants, and those\n";
                        strComment += "\tinterested in real estate properties would\n";
                        strComment += "\tbe familiar with the place.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Location
                        elmProperty = docProperties.CreateElement("Location");
                        txtProperty = docProperties.CreateTextNode(Location);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create some attributes for the Location element
                        atrProperty = docProperties.CreateAttribute("Address");
                        atrProperty.Value = Address;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("City");
                        atrProperty.Value = City;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("State");
                        atrProperty.Value = State;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("ZIPCode");
                        atrProperty.Value = ZIPCode;
                        elmProperty.Attributes.Append(atrProperty);
    
                        strComment = "Stories is the number\n";
                        strComment += "\tof levels (in other countries such as AU).\n";
                        strComment += "\tThis includes the basement (if any), the\n";
                        strComment += "\tfloor level, and the upper level(s), if any.\n";
                        strComment += "\tMost new constructions of single families in\n";
                        strComment += "\tUS, CA, DE, GB, and AU have three stories but you\n";
                        strComment += "\tmust check. Most condos have one story but it\n";
                        strComment += "\tis not uncommon for a condo to have 2 stories.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Stories
                        elmProperty = docProperties.CreateElement("Stories");
                        txtProperty = docProperties.CreateTextNode(Stories.ToString());
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        strComment = "A bedroom is a clearly designated area made for\n";
                        strComment += "\tsleeping. It is easily identifiable in any\n";
                        strComment += "\tconstruction. Still, if the basement of a house\n";
                        strComment += "\tis finished, unless it is clearly made as a\n";
                        strComment += "\tbedroom, it should not be counted as such.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Bedrooms
                        elmProperty = docProperties.CreateElement("Bedrooms");
                        txtProperty = docProperties.CreateTextNode(Bedrooms.ToString());
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        strComment = "In our real estate listing, there are two\n";
                        strComment += "\ttypes of bathrooms, full and half:\n";
                        strComment += "\t* A bathroom is counted as full if it is\n";
                        strComment += "\t\tequipped with either a shower area, a bath tub,\n";
                        strComment += "\t\tor both. In addition, it should have toilet\n";
                        strComment += "\t\tand a sink\n";
                        strComment += "\t* A bathroom is considered half if it (typically)\n";
                        strComment += "\t\thas neither a shower area nor a bath tub.\n";
                        strComment += "\t\tIn most single family and townhouses, a half bath\n";
                        strComment += "\t\tis constructed on the floor level to\n";
                        strComment += "\t\tconveniently serve the visitors who enter the house.\n";
                        strComment += "\t\tA typical half bathroom has a toilet (to\n";
                        strComment += "\t\tdo the business) and a sink to wash the hands.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Bathrooms
                        elmProperty = docProperties.CreateElement("Bathrooms");
                        txtProperty = docProperties.CreateTextNode(Bathrooms.ToString("F"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        strComment = "The value of the property, also referred to as\n";
                        strComment += "\tthe market value or the listing value, can be\n";
                        strComment += "\tthe asking price for the property.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named MarketValue
                        elmProperty = docProperties.CreateElement("MarketValue");
                        txtProperty = docProperties.CreateTextNode(MarketValue.ToString("F"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        strComment = "The description of a property gives an overview\n";
                        strComment += "\tof what the property offers. It is\n";
                        strComment += "\tusually written with positive words that\n";
                        strComment += "\tcan entice, we mean invite :), a pontential\n";
                        strComment += "\tbuyer to feel comfortable with the idea of\n";
                        strComment += "\tacquiring the property.";
                        cmtDescription = docProperties.CreateComment(strComment);
                        elmRoot.LastChild.AppendChild(cmtDescription);
    
                        // Create a node named Description
                        elmProperty = docProperties.CreateElement("Description");
                        txtProperty = docProperties.CreateTextNode(Description);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Save the XML file
                        docProperties.Save(strFilename);
                    }
                    else
                    {
                        // Open the XML file
                        docProperties.Load(strFilename);
    
                        // Get a reference to the root element
                        elmRoot = docProperties.DocumentElement;
                        // Create a node named Property
                        XmlElement elmProperty = docProperties.CreateElement("Property");
    
                        // Create an attribute for the Propety node
                        elmProperty.SetAttribute("Code", PropertyCode);
    
                        // Create an attribute for the Propety node
                        elmProperty.SetAttribute("Status", SaleStatus);
                        // Add it to the root element
                        elmRoot.AppendChild(elmProperty);
    
                        // Create a node named Property
                        elmProperty = docProperties.CreateElement("DateListed");
                        XmlText txtProperty = docProperties.CreateTextNode(DateListed.ToString("d"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
                        elmRoot.LastChild.AppendChild(elmProperty);
    
                        // Create an attribute for the DateListed element
                        XmlAttribute atrProperty = docProperties.CreateAttribute("YearBuilt");
                        atrProperty.Value = YearBuilt.ToString();
                        elmProperty.SetAttributeNode(atrProperty);
    
                        // Create a node named PropertyType
                        elmProperty = docProperties.CreateElement("PropertyType");
                        txtProperty = docProperties.CreateTextNode(PropertyType);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create an attribute for the PropertyType element
                        atrProperty = docProperties.CreateAttribute("Style");
                        atrProperty.Value = Style;
                        elmProperty.SetAttributeNode(atrProperty);
    
                        // Create an attribute for the PropertyType element
                        atrProperty = docProperties.CreateAttribute("Condition");
                        atrProperty.Value = Condition;
                        elmProperty.SetAttributeNode(atrProperty);
    
                        // Create a node named Location
                        elmProperty = docProperties.CreateElement("Location");
                        txtProperty = docProperties.CreateTextNode(Location);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create some attributes for the Location element
                        atrProperty = docProperties.CreateAttribute("Address");
                        atrProperty.Value = Address;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("City");
                        atrProperty.Value = City;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("State");
                        atrProperty.Value = State;
                        elmProperty.Attributes.Append(atrProperty);
    
                        atrProperty = docProperties.CreateAttribute("ZIPCode");
                        atrProperty.Value = ZIPCode;
                        elmProperty.Attributes.Append(atrProperty);
    
                        // Create a node named Stories
                        elmProperty = docProperties.CreateElement("Stories");
                        txtProperty = docProperties.CreateTextNode(Stories.ToString());
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create a node named Bedrooms
                        elmProperty = docProperties.CreateElement("Bedrooms");
                        txtProperty = docProperties.CreateTextNode(Bedrooms.ToString());
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create a node named Bathrooms
                        elmProperty = docProperties.CreateElement("Bathrooms");
                        txtProperty = docProperties.CreateTextNode(Bathrooms.ToString("F"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create a node named MarketValue
                        elmProperty = docProperties.CreateElement("MarketValue");
                        txtProperty = docProperties.CreateTextNode(MarketValue.ToString("F"));
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Create a node named Description
                        elmProperty = docProperties.CreateElement("Description");
                        txtProperty = docProperties.CreateTextNode(Description);
                        elmRoot.LastChild.AppendChild(elmProperty);
                        elmRoot.LastChild.LastChild.AppendChild(txtProperty);
    
                        // Save the XML file
                        docProperties.Save(strFilename);
                    }
    
                    if (strPicturePath.Length != 0)
                    {
                        // The following code gets a reference to the picture's name 
                        FileInfo flePicture = new FileInfo(strPicturePath);
                        // Then it copies it to the directory of this business
                        // It changes its name to be the same as the property code
                        flePicture.CopyTo(@"C:\Altair Realtors\" +
                                          PropertyCode +
                                          flePicture.Extension);
                    }
    
                    ShowListing();
    
                    if (lvwProperties.Items.Count > 0)
                        lvwProperties.Items[0].Selected = true;
                }
            }
    
            void ShowListing()
            {
                XmlDocument docProperties = new XmlDocument();
                string strFilename = @"C:\Altair Realtors\properties.xml";
    
                if (File.Exists(strFilename))
                {
                    lvwProperties.Items.Clear();
    
                    docProperties.Load(strFilename);
                    XmlElement elmProperty = docProperties.DocumentElement;
    
                    XmlNodeList lstProperties = elmProperty.GetElementsByTagName("Property");
                    XmlNodeList lstPropertyTypes = elmProperty.GetElementsByTagName("PropertyType");
                    XmlNodeList lstLocations = elmProperty.GetElementsByTagName("Location");
                    XmlNodeList lstStories = elmProperty.GetElementsByTagName("Stories");
                    XmlNodeList lstBedrooms = elmProperty.GetElementsByTagName("Bedrooms");
                    XmlNodeList lstBathrooms = elmProperty.GetElementsByTagName("Bathrooms");
                    XmlNodeList lstMarketValues = elmProperty.GetElementsByTagName("MarketValue");
    
                    for (int i = 0; i < lstProperties.Count; i++ )
                    {
                        ListViewItem lviProperty = new ListViewItem((i + 1).ToString());
    
                        lviProperty.SubItems.Add(lstProperties[i].Attributes[0].InnerText); // Property Code
                        lviProperty.SubItems.Add(lstPropertyTypes[i].InnerText); // Property Type
                        lviProperty.SubItems.Add(lstPropertyTypes[i].Attributes[1].InnerText); // Property Condition
                        lviProperty.SubItems.Add(lstLocations[i].InnerText);
                        lviProperty.SubItems.Add(lstStories[i].InnerText);
                        lviProperty.SubItems.Add(lstBedrooms[i].InnerText);
                        lviProperty.SubItems.Add(lstBathrooms[i].InnerText);
                        lviProperty.SubItems.Add(lstMarketValues[i].InnerText);
    
                        lvwProperties.Items.Add(lviProperty);
                    }
    
                    txtDescription.Text = lstProperties[0]["Description"].InnerText;
                }
            }
    
            private void AltairRealtor_Load(object sender, EventArgs e)
            {
                ShowListing();
    
                if( lvwProperties.Items.Count > 0 )
                    lvwProperties.Items[0].Selected = true;
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void lvwProperties_SelectedIndexChanged(object sender, EventArgs e)
            {
                if ((lvwProperties.SelectedItems.Count == 0) ||
                    (lvwProperties.SelectedItems.Count > 1))
                    return;
    
                string strPropertyCode = lvwProperties.SelectedItems[0].SubItems[1].Text;
    
                // Make a list of the picture files
                string strDirectory = @"C:\Altair Realtors";
                DirectoryInfo dirProperties = new DirectoryInfo(strDirectory);
                FileInfo[] PictureFiles = dirProperties.GetFiles();
    
                // Look for a file that holds the same name as the item number
                foreach (FileInfo fle in PictureFiles)
                {
                    // Get the name of the file without its extension
                    string fwe = Path.GetFileNameWithoutExtension(fle.FullName);
    
                    if (fwe == strPropertyCode)
                        pbxProperty.Image = Image.FromFile(strDirectory +
                    "\\" + strPropertyCode + fle.Extension);
                }
    
                XmlDocument docProperties = new XmlDocument();
                string strFilename = @"C:\Altair Realtors\properties.xml";
    
                if (File.Exists(strFilename))
                {
                    docProperties.Load(strFilename);
                    XmlElement elmProperty = docProperties.DocumentElement;
                    XmlNodeList lstProperties = elmProperty.GetElementsByTagName("Property");
    
                    for (int i = 0; i < lstProperties.Count; i++ )
                    {
                        if (lstProperties[i].Attributes[0].InnerText == strPropertyCode)
                            txtDescription.Text = lstProperties[i]["Description"].InnerText;
                    }
                }
            }
        }
    }

Exercises

 

Altair Realtors

  1. Create a Windows Application named AltairRealtors5a following the same steps as this lesson
  2. Create big pictures of the properties (houses) and saved them
  3. Configure the first form so that, if the user double-clicks the picture, another window opens and displays a bigger picture of the house or of the building
  4. Configure the first form so that, if the user double-clicks an item, the property editor opens and displays the corresponding property. Then, if the user modifies a value in the dialog box and clicks OK, update the same property with the changed value(s)
  5. Make the necessary changes to the application to produce an XML with the following additional elements and attributes
    <Listing>
        <Property Code"000-000" Status="">
            <DateListed YearBuilt=""></DateListed>
            <PropertyType Style="" Condition=""></PropertyType>
            <Location Address="" City="" State="" ZIPCode=""></Location>
            <Stories></Stories>
            <Bedrooms UpperFloor="4" LowerFloor="0" Basement="1">5</Bedrooms>
            <Bathrooms UpperFloor="2" LowerFloor="0.5" Basement="1">3.5</Bathrooms>
            <IndoorGarage Spaces="2">true</IndoorGarage>
            <FinishedBasement Entrance="Common/Rear" FinishedType="Recreation Room/Apartment">true</FinishedBasement>
            <Description></Description>
        </Property>
    </Listing>
 
 

Home Copyright © 2010-2011 C#Key Home