從DataSet 返回XML 數據

DataSet 常用操作Xml方法有DataSet.WriteXml()、DataSet.ReadXml()、DataSet.GetXml()

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Xml.XPath;
namespace UsingDataSet
{
    public partial class Form1 : Form
    {
        string strSQL = null;
        DataSet ds = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void loadFromToolStripMenuItem_Click(object sender, EventArgs e)
        {
            strSQL = @"SELECT  [ProductID]
                              ,[ProductName]
                              ,[SupplierID]
                              ,[CategoryID]
                              ,[QuantityPerUnit]
                              ,[UnitPrice]
                              ,[UnitsInStock]
                              ,[UnitsOnOrder]
                              ,[ReorderLevel]
                              ,[Discontinued]
                          FROM [Products]";
           ds =SqlHelper.ExecuteDataSet(CommandType.Text, strSQL);
           dataGridView1.DataSource = ds.Tables[0];

        }    
       

        //WriteXml
        private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (DialogResult.OK == sfd.ShowDialog())
            {
                string fileName = sfd.FileName;
                //this.ds.WriteXml(fileName, XmlWriteMode.WriteSchema);
                this.ds.WriteXml(fileName, XmlWriteMode.DiffGram);
            }
        }
        //ReadXml
        private void loadFromFileStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (DialogResult.OK == ofd.ShowDialog())
            {
                string fileName = ofd.FileName;
                this.ds.ReadXml(fileName, XmlReadMode.ReadSchema);
                dataGridView1.DataSource = this.ds.Tables[0];
            }
        }

        //GetXml
        private void getXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.ds.GetXml());
        }       
       
    }
}

發佈了81 篇原創文章 · 獲贊 0 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章