Hi everyone. I've got this code that I've been following along with a sample. However, whenever I debug it, I get the error 'XML Exception was unhandled: Root element is missing.' Here's the code I have so far.

namespace SpiveyPropertyRental
{
    public partial class Tenants : Form
    {
        public Tenants()
        {
            InitializeComponent();
        }

        public void ShowTenants()
        {
            string strFileName = @"C:\Spivey Property Rental\tenants.xml";
            XmlDocument docTenants = new XmlDocument();

            if (File.Exists(strFileName))
            {
                lvwTenants.Items.Clear();

                docTenants.Load(strFileName);
                XmlElement elmTenant = docTenants.DocumentElement;
                XmlNodeList lstTenants = elmTenant.ChildNodes;

                foreach (XmlNode node in lstTenants)
                {
                    ListViewItem lviTenant = new ListViewItem(node.FirstChild.InnerText); //Account #

                    lviTenant.SubItems.Add(node.FirstChild.NextSibling.InnerText); //Full Name
                    lviTenant.SubItems.Add(node.FirstChild.NextSibling.NextSibling.InnerText);  //Phone #
                    lviTenant.SubItems.Add(node.FirstChild.NextSibling.NextSibling.NextSibling.InnerText);  //Marital Status
                    lvwTenants.Items.Add(lviTenant);
                }
            }
        }

        private void Tenants_Load(object sender, EventArgs e)
        {
            ShowTenants();
        }
    }
}

I'm getting the exception here-

docTenants.Load(strFileName);

That exception is a validation error. You need to provide the XML file.

[code=text] ...xml here...

[/code]

Yeah that's what's strange. I have the file in the directory specified. Strange..

No -- provide the XML file to us so we can help you. Paste it on the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.