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());
}
}
}