Dataset数据的XML持久化处理

jakefei 2020-01-23

主要方法是用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";
            }
        }

相关推荐