load listview from xml file

inkedGFX

Well-known member
Joined
Feb 2, 2013
Messages
142
Programming Experience
Beginner
Hello,

I am trying to load a listview control from a xml file...my code seems to load a few items but not all the items.....here is the code...

string doc = "gowStats.xml";


            if (!File.Exists(doc))
            {
                MessageBox.Show("file doesnt exist");
            }
            else
            {
                try
                {
                   
                    using (XmlReader reader = XmlReader.Create("gowStats.xml"))
                    {
                        int index = 0;
                        
                        while (reader.Read())
                        {
                            switch (reader.Name)
                            {
                                case "Members":
                                    members = "";
                                    break;
                                case "Handle":
                                    member = reader.ReadString();
                                    xmlItem.Add(member);
                                    break;
                                case "Rank":
                                    rank = reader.ReadString();
                                    xmlItem.Add(rank);
                                    break;
                                case "Power":
                                    power = reader.ReadString();
                                    xmlItem.Add(power);
                                    break;
                                case "Cords":
                                    cords = reader.ReadString();
                                    xmlItem.Add(cords);
                                    break;
                                case "Loyalty":
                                    loyalty = reader.ReadString();
                                    xmlItem.Add(loyalty);
                                    break;


                            }
                           
                        }
                        //foreach (var item in xmlItem)
                        //{
                        //    MessageBox.Show(item);
                        //}
                        ListViewItem lvi = new ListViewItem(xmlItem[0]);
                       // listView1.Items.Add(lvi);


                        for (int i = 1; i < xmlItem.Count; i++)
                        {
                            lvi.SubItems.Add(xmlItem[i]);
                            listView1.Items.Add(lvi);
                        }
                      
                    }
                   
                    
                }
                catch 
                {
                    return;
                }
            }


this loads the first couple items from the xmlItem list...i use the messagebox to see if all the xml elements were loaded into the list...they were....so i am a little confused as to why this doesn't work..

thank you for any help

-Inked
 
My first guess would be that an exception is being thrown but all you do in that case is end the method so how would you know? I would suggest at least writing out the exception to the Output window so you can see if one is thrown and what it is. Putting a breakpoint on the 'return' line would be an easy way to know if an exception is thrown.
 
yes...all i did was return....was meaning to change that and forgot about it...problem solved...also I had the listview code in the wrong area.... thank you for your help.....
I have another issue I could use your help on!

I want to update the data in the xml file but the code I have written isn't working....here is the entire click event method

 private void cmdUpdate_Click(object sender, EventArgs e)        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("gowStats.xml");


            //XmlNode member = xDoc.SelectSingleNode("Members\\Member");
            //XmlNode handle = xDoc.DocumentElement["Handle"];
            //XmlNode rank = xDoc.DocumentElement["Rank"];
            //XmlNode power = xDoc.DocumentElement["Power"];
            //XmlNode loyalty = xDoc.DocumentElement["Loyalty"];
            //XmlNode cords = xDoc.DocumentElement["Cords"];


            //handle.FirstChild.InnerText = txtMember.Text;
            //rank.FirstChild.InnerText = txtRank.Text;
            //power.FirstChild.InnerText = txtPower.Text;
            //loyalty.FirstChild.InnerText = txtLoyalty.Text;
            //cords.FirstChild.InnerText = txtCords.Text;


            XmlNodeList nodes = xDoc.SelectNodes("/Member");


            


            foreach (XmlNode n in nodes)
            {
                if (n.SelectSingleNode("/Handle").InnerText == txtMember.Text)
                {
                    n.SelectSingleNode("/Handle").InnerText = txtMember.Text;
                    n.SelectSingleNode("/Rank").InnerText = txtRank.Text;
                    n.SelectSingleNode("/Power").InnerText = txtPower.Text;
                    n.SelectSingleNode("/Loyalty").InnerText = txtLoyalty.Text;
                    n.SelectSingleNode("/Cords").InnerText = txtCords.Text;
                    MessageBox.Show(n.InnerText);
                }
                else
                    MessageBox.Show("Error!");
            }


          


            xDoc.Save("gowStats.xml");


            MessageBox.Show("Member Stats Updated Successfully! ", "Update Success",
                MessageBoxButtons.OK, MessageBoxIcon.Information);


            txtLoyalty.Text = "";
            txtCords.Text = "";
            txtMember.Text = "";
            txtPower.Text = "";
            txtRank.Text = "";


        }


the area commented out was my first attampt.....

thank you for any help

-InkedGFX
 
Back
Top Bottom