xml读写

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {

            List<User> list1 = new List<User>();
 
           XDocument xdoc = XDocument.Load("UserData.xml");
           XElement xroot = xdoc.Root;

           foreach (XElement item in xroot.Elements())
            {

               list1.Add(new User(){Id=item.Attribute("id")==null?"":item.Attribute("id").Value});
                
            }


           MessageBox.Show("aaa");

        }
    }



    class User
    {
        string _id;

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }
        string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<Users>
  <user >
    <!--id的值用户输入有可能重复,这是不允许重复的,为了简单操作咱们不判断了-->
    <name>周星驰</name>
    <password>xingye</password>
  </user>
  <user id="009">
    <name>老牛</name>
    <password>123</password>
  </user>
  <user id="008">
    <name>灰太狼</name>
    <password>ml</password>
  </user>
  <user id="020">
    <name>xml</name>
    <password>malun</password>
    <!--这是注释html一样的-->
  </user>
</Users>

 

posted @ 2015-12-30 21:42  刀小爱  阅读(116)  评论(0编辑  收藏  举报