Form1.cs

public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        DataTable table = new DataTable();
        //List<Pessoa> p = new List<Pessoa>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Add Columns to DataTable (table)
            table.Columns.Add("Nome", typeof(string));
            table.Columns.Add("Idade", typeof(string));
            table.Columns.Add("Profissão", typeof(string));
            table.Columns.Add("Signo", typeof(string));
            table.Columns.Add("Morada", typeof(string));
            table.Columns.Add("Telemóvel", typeof(string));
            table.Columns.Add("Email", typeof(string));

            //ListView Properties
            listView1.View = View.Details;
            listView1.GridLines = true;
            listView1.FullRowSelect = true;


            //Add Columns to ListView
            listView1.Columns.Add("Nome", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Idade", 40, HorizontalAlignment.Left);
            listView1.Columns.Add("Profissão", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Signo", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("Morada", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("Telemóvel", 80, HorizontalAlignment.Left);
            listView1.Columns.Add("Email", 120, HorizontalAlignment.Left);

            //Array for Items
            string[] array = new string[8];
            ListViewItem lvitem;

            //Add Item1 to ListView
            array[0] = "Diogo Monteiro";
            array[1] = "21";
            array[2] = "C# Developer";
            array[3] = "Peixes";
            array[4] = "Rua da Paz";
            array[5] = "910852805";
            array[6] = "mdev@mateus.work";
            lvitem = new ListViewItem(array);
            listView1.Items.Add(lvitem);

            //Add Item2 to ListView
            array[0] = "Diogo Silva";
            array[1] = "24";
            array[2] = "Storage Developer";
            array[3] = "Carneiro";
            array[4] = "Rua da Costa";
            array[5] = "914568457";
            array[6] = "diogosilva@mateus.work";
            lvitem = new ListViewItem(array);
            listView1.Items.Add(lvitem);
        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddPessoa MyForm = new AddPessoa();
            if (MyForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                Pessoa _c = MyForm._resultado;
                PessoaTable _ct = new PessoaTable();



                DataRow nr = ds.Tables[0].NewRow();
                nr["Nome"] = _c.nome;
                nr["Idade"] = _c.idade;
                nr["Profissao"] = _c.profissao;
                nr["Signo"] = _c.signo;
                nr["Morada"] = _c.morada;
                nr["Telemovel"] = _c.telemovel;
                nr["Email"] = _c.email;
                ds.Tables[0].Rows.Add(nr);

                XElement xmlNode = new XElement("Pessoa",
                new XElement("Nome", _c.nome),
                new XElement("Idade", _c.idade),
                new XElement("Profissao", _c.profissao),
                new XElement("Signo", _c.signo),
                new XElement("Morada", _c.morada),
                new XElement("Telemovel", _c.telemovel),
                new XElement("Email", _c.email));

                XElement xmlFile;
                try
                {
                    xmlFile = XElement.Load("diogomonteiro.xml");
                    xmlFile.Add(xmlNode);

                }
                catch (XmlException)
                {
                    xmlFile = new XElement("Pessoa", xmlNode);
                }
                xmlFile.Save("diogomonteiro.xml");
            }
        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnDeleteAll_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
        }

        private void tsBtnDeleteRow_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) return;
            listView1.SelectedItems[0].Remove();
        }

        private void tsBtnShowData_Click(object sender, EventArgs e)
        {
            //// Get the table from the data set
            //DataTable dtable = ds.Tables["Pessoa"];

            //// Clear the ListView control
            //listView1.Items.Clear();

            //// Display items in the ListView control
            //for (int i = 0; i < dtable.Rows.Count; i++)
            //{
            //    DataRow drow = dtable.Rows[i];

            //    // Only row that have not been deleted
            //    if (drow.RowState != DataRowState.Deleted)
            //    {
            //        // Define the list items
            //        ListViewItem lvitem = new ListViewItem(drow["Pessoa"].ToString());
            //        lvitem.SubItems.Add(drow["nome"].ToString());
            //        lvitem.SubItems.Add(drow["idade"].ToString());
            //        lvitem.SubItems.Add(drow["profissao"].ToString());
            //        lvitem.SubItems.Add(drow["signo"].ToString());
            //        lvitem.SubItems.Add(drow["morada"].ToString());
            //        lvitem.SubItems.Add(drow["telemovel"].ToString());
            //        lvitem.SubItems.Add(drow["email"].ToString());

            //        // Add the list items to the ListView
            //        listView1.Items.Add(lvitem);
            //    }
            //}
        }

        }
    }

object.cs

    public class PessoaListViewItem : ListViewItem
    {

        public override string ToString()
        {
            return Nome;
        }

        internal PessoaListViewItem(Pessoa _p)
            : base()
        {
            Nome = string.Empty;
            Idade = string.Empty;
            Profissao = string.Empty;
            Signo = string.Empty;
            Morada = string.Empty;
            Telemovel = string.Empty;
            Email = string.Empty;

            this.Text = _p.nome; //ListViewItem

            //Adicionar Sub-Items
            ListViewItem.ListViewSubItem _si1 = new ListViewItem.ListViewSubItem();
            _si1.Text = _p.idade;
            _si1.Text = _p.profissao;
            _si1.Text = _p.signo;
            _si1.Text = _p.morada;
            _si1.Text = _p.telemovel;
            _si1.Text = _p.email;

            this.SubItems.Add(_si1);
}

        private string _c { get; set; }
        public string Nome { get; set; }
        public string Idade { get; set; }
        public string Profissao { get; set; }
        public string Signo { get; set; }
        public string Morada { get; set; }
        public string Telemovel { get; set; }
        public string Email { get; set; }

        internal static void Equals()
        {
            throw new NotImplementedException();
        }
    }

    public class PessoaTable : DataTable
    {
        public PessoaTable()
        {
            Columns.Add(new DataColumn("Nome", typeof(string)));
            Columns.Add(new DataColumn("Idade", typeof(string)));
            Columns.Add(new DataColumn("Profissão", typeof(string)));
            Columns.Add(new DataColumn("Signo", typeof(string)));
            Columns.Add(new DataColumn("Morada", typeof(string)));
            Columns.Add(new DataColumn("Telemóvel", typeof(string)));
            Columns.Add(new DataColumn("Email", typeof(string)));
        }



    }


}

AddPessoa.cs

    public class PessoaListViewItem : ListViewItem
    {

        public override string ToString()
        {
            return Nome;
        }

        internal PessoaListViewItem(Pessoa _p)
            : base()
        {
            Nome = string.Empty;
            Idade = string.Empty;
            Profissao = string.Empty;
            Signo = string.Empty;
            Morada = string.Empty;
            Telemovel = string.Empty;
            Email = string.Empty;

            this.Text = _p.nome; //ListViewItem

            //Adicionar Sub-Items
            ListViewItem.ListViewSubItem _si1 = new ListViewItem.ListViewSubItem();
            _si1.Text = _p.idade;
            _si1.Text = _p.profissao;
            _si1.Text = _p.signo;
            _si1.Text = _p.morada;
            _si1.Text = _p.telemovel;
            _si1.Text = _p.email;

            this.SubItems.Add(_si1);
}

        private string _c { get; set; }
        public string Nome { get; set; }
        public string Idade { get; set; }
        public string Profissao { get; set; }
        public string Signo { get; set; }
        public string Morada { get; set; }
        public string Telemovel { get; set; }
        public string Email { get; set; }

        internal static void Equals()
        {
            throw new NotImplementedException();
        }
    }

    public class PessoaTable : DataTable
    {
        public PessoaTable()
        {
            Columns.Add(new DataColumn("Nome", typeof(string)));
            Columns.Add(new DataColumn("Idade", typeof(string)));
            Columns.Add(new DataColumn("Profissão", typeof(string)));
            Columns.Add(new DataColumn("Signo", typeof(string)));
            Columns.Add(new DataColumn("Morada", typeof(string)));
            Columns.Add(new DataColumn("Telemóvel", typeof(string)));
            Columns.Add(new DataColumn("Email", typeof(string)));
        }



    }


}

I have to Insert Object Type and Load Xml file in ListView, how ?

Re: How to Insert Object Type and Load Xml file in ListView? 80 80

Check out XmlSerializer to see if it meets your needs. See this post for how to use it.

Be a part of the DaniWeb community

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