C#用DataSet操作Xml的代码

本文介绍了一种使用.NET Framework中的DataSet类来读取和修改XML文件的方法。相较于直接使用System.Xml进行操作,此方法提供了更方便的数据管理和更新方式。文中通过一个简单的Windows Forms应用程序示例,演示了如何加载XML数据到DataSet中,并将其展示在一个DataGridView控件上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * ? SharpDevelop ???
 * ??: Administrator
 * ??: 2008-1-9
 * ??: 17:18
 *
 * ??????????,?????“?? | ?? | ?? | ??????”?
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Data;

namespace test
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
       
        public MainForm()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
           
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
       
        void MainFormLoad(object sender, EventArgs e)
        {
        }
       
        void MainFormFormClosing(object sender, FormClosingEventArgs e)
        {
//            FileStream fs = new FileStream("c:\\a.xml", FileMode.Create);
            //dataSet1.WriteXml("c:\\b.xml");
            //dataSet1.AcceptChanges();
            //DataSet ds2 = new System.Data.DataSet();
           
            //ds2.ReadXml(
            //this.dataSet1.WriteXml(fs);
            //fs.Close();
        }
       
        void Button1Click(object sender, EventArgs e)
        {
            dataSet1.ReadXml("c:\\b.xml");
//            System.Data.DataColumn[] dcColl = new System.Data.DataColumn[2];
//            dcColl[0] = new System.Data.DataColumn("Column1");
//            dcColl[1] = new System.Data.DataColumn("Column2");
//            this.dataSet1.Tables.Add("table1");
//            this.dataSet1.Tables["table1"].Columns.AddRange(dcColl);
            this.dataGrid1.DataSource = this.dataSet1;
   
        }
       
        void Button2Click(object sender, EventArgs e)
        {
            //textBox1.Text = dataSet1.GetXml();
        }
    }
}

如题。。

写的比较粗糙,以前一直用System.Xml的

发现这种方式更好~

用空会整理整理,写个完整的创建、读取、修改Xml文件的完整例子上来的~

转载于:https://www.cnblogs.com/shiweifu/archive/2008/01/09/1032372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值