Dataset数据的XML持久化处理

主要方法是用ADO.NET的DataTale 填充到Dataset

Dataset 内置了XML持久化的方法,WriteXML和ReadXML:简单的WinFrom实例:从数据库的表

private void button1_Click(object sender, EventArgs e)
        {

            string path = AppDomain.CurrentDomain.BaseDirectory+"Users.xml";//BIN/DEBUGA
            string sql = "SELECT * FROM Users";
            DataTable td = DBHelper.Query(sql, null);
            DataSet ds = new DataSet();//查询填充DATABLE到DTAASET
            ds.Tables.Add(td);
            ds.WriteXml(path);//保存到本地
        
            XmlDocument xmld = new XmlDocument();
            xmld.Load(path);
            XmlNode root = xmld.DocumentElement;
            label1.Text = "";
            for (int i = 0; i < root.ChildNodes.Count; i++)
            {
                label1.Text = root.ChildNodes[i].InnerText+ "\r\n ";
            }
            //string xml = ds.GetXml(); 只读  不本地创建方法可以用
            //textBox1.Text = xml;
        }

下面一种方法是带路径的默认路径

 private void button2_Click(object sender, EventArgs e)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(@"C:\Users\Administrator\Desktop\PointManagement\Point.UI\Book.xml");
            XmlNode root = xml.DocumentElement;

            textBox2.Text = "";
            for (int i = 0; i < root.ChildNodes.Count; i++)
            {
                textBox2.Text = root.ChildNodes[i].ChildNodes[1].InnerText + "\r\n";
            }
        }
posted @ 2020-01-23 15:32  Aquiet  阅读(84)  评论(0编辑  收藏