C#将xml文件数据导入datagridview中

来源:互联网 发布:阿里云rds是什么 编辑:程序博客网 时间:2024/05/29 18:02
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string strPath=null;//文件咱径
        private void button1_Click(object sender, EventArgs e)
        {
           
            openFileDialog1.Filter = "xml文件|*.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                strPath = openFileDialog1.FileName;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (strPath != null)
            {
                DataSet ds = new DataSet();
                ds.ReadXmlSchema(strPath);
                ds.ReadXml(strPath);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            else
            {
                MessageBox.Show("请选择XML文件");
            }
        }
原创粉丝点击