Question content of XML file not loading correctly

New_To_Coding

New member
Joined
Mar 2, 2016
Messages
2
Programming Experience
Beginner
This is my main form class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace ContactList_AT2Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
           
            SeriList.Deserialize(Contact, "XmlContacts.xml");
            


        }


        List<ContactInfo> Contact = new List<ContactInfo>();


        private void button1_Click(object sender, EventArgs e)
        {


            ContactInfo updateInfo = new ContactInfo();
            updateInfo.Surname = tbSurname.Text;
            updateInfo.Firstname = tbFirstname.Text;
            updateInfo.Address = tbAddress.Text;
            updateInfo.Suburb = tbSuburb.Text;
            updateInfo.Postcode = tbPostcode.Text;
            updateInfo.Phone = tbPhone.Text;
            updateInfo.Mobile = tbMobile.Text;


            if (lbContacts.SelectedIndex == -1)
            {
                MessageBox.Show("You must select a contact to update");
            }
            else
            {
                string selItem = lbContacts.SelectedItem.ToString();
                int index = lbContacts.FindString(selItem);
                Contact[index].Surname = updateInfo.Surname;
                Contact[index].Firstname = updateInfo.Firstname;
                Contact[index].Address = updateInfo.Address;
                Contact[index].Suburb = updateInfo.Suburb;
                Contact[index].Postcode = updateInfo.Postcode;
                Contact[index].Phone = updateInfo.Phone;
                Contact[index].Mobile = updateInfo.Mobile;
                DisplayContactList();
            }
        }




        private void Reset()
        {
            tbSurname.Text = "";
            tbFirstname.Text = "";
            tbAddress.Text = "";
            tbSuburb.Text = "";
            tbPostcode.Text = "";
            tbPhone.Text = "";
            tbMobile.Text = "";
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lbContacts.SelectedIndex == -1)
            {
                MessageBox.Show("Select a contact to delete");
            }
            else
            {
                string selItem = lbContacts.SelectedItem.ToString();
                int index = lbContacts.FindString(selItem);
                Contact.RemoveAt(index);
                DisplayContactList();
                Reset();


            }


        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool hasData = true;


            ContactInfo newContact = new ContactInfo();


            newContact.Surname = tbSurname.Text;
            newContact.Firstname = tbFirstname.Text;
            newContact.Address = tbAddress.Text;
            newContact.Suburb = tbSuburb.Text;
            newContact.Postcode = tbPostcode.Text;
            newContact.Phone = tbPostcode.Text;
            newContact.Mobile = tbMobile.Text;


            if (String.IsNullOrEmpty(tbSurname.Text))
            {
                MessageBox.Show("Enter a surname");
                hasData = false;
                return;
            }
            if (String.IsNullOrEmpty(tbFirstname.Text))
            {
                MessageBox.Show("Enter a firstname");
                hasData = false;
                return;
            }
            if (String.IsNullOrEmpty(tbPhone.Text) && (String.IsNullOrEmpty(tbMobile.Text)))
            {
                MessageBox.Show("You need to enter on contact number");
                hasData = false;
                return;
            }


            bool duplicate = Contact.Exists(x => x.Surname + x.Firstname == tbSurname.Text + tbFirstname.Text);


            if (hasData && !duplicate)
            {
                Contact.Add(newContact);
                SortList();
                Reset();
                DisplayContactList();
            }
            else
            {
                MessageBox.Show("That name exists already");
            }
        }


        public void DisplayContactList()
        {
            lbContacts.Items.Clear();
  
            foreach (var con in Contact)
            {
                lbContacts.Items.Add(con.Surname + ", " + con.Firstname);
            }
        }


        public void SortList()
        {
            
            Contact.Sort();
            DisplayContactList();
        }


        private void lbContacts_MouseClick(object sender, MouseEventArgs e)
        {
            if (lbContacts.SelectedIndex == -1)
            {
                MessageBox.Show("Select a contact name to display information");
            }
            else
            {
                string selItem = lbContacts.SelectedItem.ToString();
                int index = lbContacts.FindString(selItem);
                lbContacts.SetSelected(index, true);
                tbSurname.Text = Contact[index].Surname;
                tbFirstname.Text = Contact[index].Firstname;
                tbAddress.Text = Contact[index].Address;
                tbSuburb.Text = Contact[index].Suburb;
                tbPhone.Text = Contact[index].Phone;
                tbMobile.Text = Contact[index].Mobile;
            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            SeriList.SerializeObject(Contact, "XmlContacts.xml");
        }


        private void lbContacts_SelectedIndexChanged(object sender, EventArgs e)
        {
      
        }
    }
}



This is the list class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Threading;


namespace ContactList_AT2Project
{
    [Serializable()]


    public class ContactInfo : IComparable<ContactInfo>
    {




        [System.Xml.Serialization.XmlElement("Surname")]
        public string Surname
        {
            get;
            set;
        }


        [System.Xml.Serialization.XmlElement("Firstname")]
        public string Firstname
        {
            get;
            set;
        }


        [System.Xml.Serialization.XmlElement("Address")]
        public string Address
        {
            get;
            set;
        }


        [XmlElement("Suburb")]
        public string Suburb
        {
            get;
            set;
        }


        [XmlElement("Postcode")]
        public string Postcode
        {
            get;
            set;
        }


        [XmlElement("Phone")]
        public string Phone
        {
            get;
            set;
        }


        [XmlElement("Mobile")]
        public string Mobile
        {
            get;
            set;
        }
        public int CompareTo(ContactInfo other)
        {
            return this.Surname.CompareTo(other.Surname);
        }




    }
}


And this is my Serialization class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Threading;


namespace ContactList_AT2Project
{


    public static class SeriList
    {


        public static void SerializeObject(this List<ContactInfo> list, string filename)
        {
            var serializer = new XmlSerializer(typeof(List<ContactInfo>));
            using (var stream = File.OpenWrite(filename))
            {
                serializer.Serialize(stream, list);
            }
        }


        public static void Deserialize(this List<ContactInfo> list, string filename)
        {
                var serializer = new XmlSerializer(typeof(List<ContactInfo>));
                using (var stream = File.OpenRead(filename))
                {
                    var other = (List<ContactInfo>)(serializer.Deserialize(stream));
                    list.Clear();
                    list.AddRange(other);
                }
            }
        }
    }



can someone please help me. My form is working pretty good but when the program is first loaded the content (in this case the contact's information ie first name last name mobile number) is not being displayed in the list box right away. But if I type a new contact details into the textbox(s) and press the add button, the new contacts name will be displayed in the listbox as well as all the saved contact names from the XML file will then show up as well. It is like the DisplayConcatcs() method is only working on the press of the add button.

I would like the names form the XML file to be displayed in the list box at run time thank you all for any help in advance.
 
Last edited by a moderator:
I doubt that anyone is going to read all that code. I'm certainly not, even after I've fixed the formatting for you. You should be posting only relevant code. I find it difficult to believe that the entire code of your form is relevant to an issue reading XML data.
 
Thank you jmcilhinney for your reply. In the future I will not add all code and only add the relevant code (once I know what the relevant code is) to the thread only. I how ever have not found a solution to my problem, so if any one out there could please help me. The problem is that the contacts names that are saved to the xml file are not showing up in the listbox when the program in first run. Once the user creates a new contact and add's this information all of a sudden all the saved contacts names appear in the list box. Any help would be greatly appreciated.. And sorry for posting all my code I have been learning programming and involved in programming for about 5 weeks so sorry if I though all my code was relevant. I am I am welder by trade and have just enrolled in a programming course to try and diverse my skill set and learn something new.
 
Back
Top Bottom