.NET环境中,XML是数据交换的基本格式,In fact ,The xml is the base of the ADO.NET,The DataSet store the data with the Xml inside it,and has many methods to write and read the xml
For example:
1.System.Data.DataSet的ReadXmlSchema()和WriteXmlSchema(),可以把数据集的结构导出导入为XML模式!
2.可以使用ReadXml()和WriteXml()读取数据集中的数据并且转换为Xml格式!与另一程序交换数据或者制作数据集的本地副本时很有效 !
3.可以把数据集绑定到xml文档上(System.Xml.XmlDocument的实例!),数据集和数据文档是同步的!可以使用ADO.NET 和XML操作来修改它!
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
string qry = @"select productname,unitprice from products";
try
{
//Create the DataAdapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry,conn);
//Open the connection
conn.Open();
//Create the DataSet
DataSet ds = new DataSet();
da.Fill(ds,"products");
//Extract the dataset to the xml
ds.WriteXml(@"f:\xml\products.xml");
}
catch (SqlException ex)
{
Console.WriteLine("The error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}