怎么用XML做数据库c#

来源:互联网 发布:大数据系统 安全防护 编辑:程序博客网 时间:2024/04/29 05:29
xml实际就是一个本地简单的数据库
我只做了一个简单的。。但是道理是一样的。
//xml文件信息
<abc>
  <Field>100</Field>
  <item>
    <id>1</id>
    <name>zhangsan</name>
    <sex>男</sex>
  </item>
  <item>
    <id>2</id>
    <name>lisi</name>
    <sex>男</sex>
  </item>
</abc>
 
//实体类。
 public class Information
    {
        private string id;
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string sex;
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }
        public Information()
        { 
            
        }
        public Information(string id,string name,string sex)
        {
            this.Id = id;
            this.Name = name;
            this.Sex = sex;
        }
    }
//读取xml里面的文件信息
 List<Information> list = new List<Information>();
            //实例化xml
            XmlDocument xml = new XmlDocument();
            //读取xml文件
            xml.Load(@"E:\C#\S2C#\DLCL\打印电脑\MyComputer\XulieHua\XML.xml");  //你的xml地址
            string id = "";
            string name = "";
            string sex = "";
            Information info = null;
            //////////*******下面开始循环读取xml文件信息********/
            ///////////////
            foreach (XmlNode node in xml.ChildNodes)
            {
                if (node.Name == "abc")
                {
                    foreach (XmlNode node1 in node.ChildNodes)
                    {
                        if (node1.Name == "item")
                        {
                            foreach (XmlNode node2 in node1.ChildNodes)
                            {
                                switch (node2.Name)
                                {
                                    case "id":
                                        id = node2.InnerText;
                                        break;
                                    case "name":
                                        name = node2.InnerText;
                                        break;
                                    default:
                                        sex = node2.InnerText;
                                        break;
                                }
                            }
                            info = new Information(id, name, sex);
                            //将信息保存至集合
                            list.Add(info);
                        }
                    }
                }
            }
xml里面的所有信息就是在list集合里面了。。简单吧。。嘿嘿。。
   当然你可以做多个表和多个字段属性咯。。
原创粉丝点击