创建XML和XML读到Dictionary

namespace ListBox和XML操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, person> dic = new Dictionary<string, person>();
        private void button1_Click(object sender, EventArgs e)
        {
            AddData();
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    c.Text = string.Empty;
                }
            }


        }
        /// <summary>
        /// 给Dictionary赋值
        /// </summary>
        private void AddData()
        {
            person p = new person(txtID.Text, txtName.Text, int.Parse(txtAge.Text), txtQQ.Text);
            if (dic.ContainsKey(p.ID))
            {
                dic[p.ID] = p;
            }
            else
            {
                dic.Add(p.ID, p);
                listBox1.Items.Add(p);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveXml();
        }
        /// <summary>
        /// 创建XML并保存
        /// </summary>
        private void SaveXml()
        {
            XDocument doc = new XDocument();
            XDeclaration dec = new XDeclaration("1.0", "utf-8", null);
            doc.Declaration = dec;
            XElement person = new XElement("person");
            foreach (KeyValuePair<string, person> temp in dic)
            {
                XElement list = new XElement("list");
                list.SetElementValue("id", temp.Value.ID);
                list.SetElementValue("name", temp.Value.name);
                list.SetElementValue("age", temp.Value.age);
                list.SetElementValue("QQ", temp.Value.QQ);
                person.Add(list);
            }
            doc.Add(person);
            doc.Save("1.xml");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                person p = listBox1.SelectedItem as person;
                txtID.Text = p.ID;
                txtName.Text = p.name;
                txtAge.Text = p.age.ToString();
                txtQQ.Text = p.QQ;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                XDocument docLoad = XDocument.Load("1.xml");//要先创建才可以读,我这try catch就不会报错
                XElement element = docLoad.Root;
                foreach (XElement temp in element.Elements())
                {
                    person p = new person();
                    p.ID = temp.Element("id").Value;
                    p.name = temp.Element("name").Value;
                    p.age = Convert.ToInt32(temp.Element("age").Value);
                    p.QQ = temp.Element("QQ").Value;
                    if (!dic.ContainsKey(p.ID))
                    {
                        dic.Add(p.ID, p);
                    }


                    listBox1.Items.Add(p);
                }                
            }
            catch (Exception ex)
            {

            }

        }
    }
}
namespace ListBox和XML操作
{
    public class person
    {
        public person()
        {
        }
        public person(string id, string name, int age, string QQ)
        {
            this.ID = id;
            this.name = name;
            this.age = age;
            this.QQ = QQ;
        }
        public string ID
        {
            get;
            set;
        }
        public string name
        {
            get;
            set;
        }
        public int age
        {
            get;
            set;
        }
        public string QQ
        {
            get;
            set;
        }
        public override string ToString()//有这个 输出的不会是 ListBox和XML操作.person
        {
            return this.name;
        }
    }
}
源码下载地址: https://pan.baidu.com/s/1IbB__9rx-4BaVRyzQESHGg 提取码: th8g

评论