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..

This article has been dead for over six months. Start a new discussion instead.