.NET中的DataSet与XML之间的关系,是本文讨论的重点。工作中我们经常使用XML 作为存储和传输各种数据的格式。
    在.NET Framework 中,经常使用XML 作为存储和传输各种数据的格式。
    DataSet 中的数据可以转换成XML 的形式来表示和存储。
    我们可以使用XML 对象同步和转换DataSet 的数据,而DataSet 也可以存储和传输XML 格式的数据。
    XML 与 DataSet 的关系如下图所示:
    详解XML与DataSet对象的关系_DataSet
    DataSet 对象的常用方法如下:
    A.  使用ReadXml( ) 方法:从文件或流中加载XML 数据,填充DataSet 对象。DataSet 对象.ReadXML( 文件路径字符串|stream 对象, XmlReadMode 枚举值[可以省略] ) ;
    B.  使用WriteXml( ) 方法:将DataSet 对象中的数据以XML 格式写出到文件或流中。DataSet 对象.WriteXml( 文件路径字符串| stream 对象, XmlWriteMode 枚举值[可以省略] ) ;
    C.  使用ReadXmlSchema( ) 方法:将Shema 模式文件读入DataSet 对象。DataSet 对象.ReadXmlSchema( Stream | FileName | TextReader | XmlReader ) ;
    D.  使用WriteXmlSchema( ) 方法:将DataSet 对象的Shema 模式文件写出到文件或流。DataSet 对象.WriteXmlSchema( Stream | FileName | TextWriter | XmlWriter ) ;
    E.  使用GetXmlSchema( ) 方法:将DataSet 对象的Shema 模式,以字符串的形式获得。DataSet 对象.GetXmlSchema( );
    F.  使用GetXml( ) 方法:将DataSet 对象的XML 格式的数据集,以字符串的形式获得。DataSet 对象.GetXml( );
    接下来,通过一个综合示例进行演示。
    Person.xml 文件如下:
    1. <?xml version="1.0" encoding="UTF-8"?>   
    2. <Persons>   
    3.    <person>   
    4.      <ID>0</ID>   
    5.      <Name>Mark</Name>   
    6.      <Age>18</Age>   
    7.    </person>   
    8.    <person>   
    9.      <ID>1</ID>   
    10.      <Name>Jorn</Name>   
    11.      <Age>22</Age>   
    12.    </person>   
    13.    <person>   
    14.      <ID>2</ID>   
    15.      <Name>Aderson</Name>   
    16.      <Age>30</Age>   
    17.    </person>   
    18. </Persons>  
    Customer.xsd 文件如下:
    1. <?xml version="1.0" encoding="UTF-8"?>   
    2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schema-microsoft-com:xml-msdata" elementFormDefault="qualified" attributeFormDefault="unqualified" id="Customers">   
    3.     <xs:element name="Customers" msdata:IsDataSet="true" msdata:EnforceConstraints="False">   
    4.         <xs:complexType>   
    5.             <xs:choice maxOccurs="unbounded">   
    6.                 <xs:element name="Customer" type="customersType"/>   
    7.             </xs:choice>   
    8.         </xs:complexType>   
    9.     </xs:element>   
    10.     <xs:complexType name="customersType">   
    11.         <xs:sequence>   
    12.             <xs:element name="CustomersID" type="xs:string" minOccurs="0"/>   
    13.             <xs:element name="CustomersName" type="xs:string" minOccurs="0"/>   
    14.             <xs:element name="CustomersAge" type="xs:int" minOccurs="0"/>   
    15.         </xs:sequence>   
    16.     </xs:complexType>   
    17. </xs:schema>  
    Winform 程序的源代码如下:
    1. namespace DataSet_XML_Demo   
    2. {   
    3.     public partial class Form1 : Form   
    4.     {   
    5.         public Form1()   
    6.         {   
    7.             InitializeComponent();   
    8.         }   
    9.         DataSet ds = new DataSet();   
    10.     
    11.         //读取XML文档的数据到DataSet   
    12.         private void btnReadXML_Click(object sender, EventArgs e)   
    13.         {   
    14.             ds.ReadXml("http://www.cnblogs.com/" + "Person.xml");   
    15.             dataGridView1.DataSource = ds.Tables[0];   
    16.         }   
    17.     
    18.         //将DataSet中的数据写出到XML文档   
    19.         private void btnWriteXML_Click(object sender, EventArgs e)   
    20.         {   
    21.             ds.WriteXml("http://www.cnblogs.com/New.xml");   
    22.  ds.WriteXml("http://www.cnblogs.com/New_Alter.xml", XmlWriteMode.DiffGram);   
    23.         }   
    24.     
    25.         //加载Schema给DataSet   
    26.         private void btnReadXmlSchema_Click(object sender, EventArgs e)   
    27.         {   
    28.             DataSet newDataSet = new DataSet();   
    29.             newDataSet.ReadXmlSchema("http://www.cnblogs.com/Customer.xsd");   
    30.             dataGridView1.DataSource = newDataSet.Tables[0];   
    31.         }   
    32.     
    33.         //将DataSet的Schema写出   
    34.         private void btnWriteXmlSchema_Click(object sender, EventArgs e)   
    35.         {   
    36.             DataSet newDataSet = new DataSet();   
    37.             DataTable dt = new DataTable();   
    38.             DataColumn dc1 = new DataColumn("id"typeof(int));   
    39.             DataColumn dc2 = new DataColumn("name"typeof(string));   
    40.             dt.Columns.Add(dc1);   
    41.             dt.Columns.Add(dc2);   
    42.             newDataSet.Tables.Add(dt);   
    43.     
    44.             dataGridView1.DataSource = newDataSet;   
    45.             dataGridView1.DataMember = "Table1";   
    46.   newDataSet.WriteXmlSchema("http://www.cnblogs.com/newSchema.xsd");   
    47.         }   
    48.     
    49.         //GetXml()方法的使用   
    50.         private void btnGetXml_Click(object sender, EventArgs e)   
    51.         {   
    52.             DataSet newXml = new DataSet();   
    53.             newXml.ReadXml("http://www.cnblogs.com/" + "Person.xml");   
    54.             dataGridView1.DataSource = newXml.Tables[0];   
    55.     
    56.             //GetXml():返回DataSet中XML形式的字符串   
    57.             string strXml = newXml.GetXml();   
    58.             textBox1.Text = strXml;   
    59.         }   
    60.     
    61.         //GetXmlSchema()方法的使用   
    62.         private void btnGetXmlSchema_Click(object sender, EventArgs e)   
    63.         {   
    64.              /* 注意:   
    65.                 如果DataSet已经拥有一个Schema模式,   
    66.                 再加载新的Schema模式文件,   
    67.                 则会自动将两个Schema模式合并。   
    68.              */   
    69.             DataSet newSchema = new DataSet();   
    70.       newSchema.ReadXmlSchema("http://www.cnblogs.com/Customer.xsd");   
    71.             dataGridView1.DataSource = newSchema.Tables[0];   
    72.     
    73.             //GetXmlSchema():返回DataSet所使用的Schema模式文件的字符串   
    74.             string strSchema = newSchema.GetXmlSchema();   
    75.             textBox1.Text = strSchema;   
    76.         }   
    77.     }   
    78. }  
    Winform 程序的界面效果如下:
    详解XML与DataSet对象的关系_DataSet_02 
    原文标题:XML 与DataSet 对象的关系
    链接:http://www.cnblogs.com/xugang/archive/2010/09/16/1827988.html
    【编辑推荐】
    1. W3C发布XProc规范 XML企业级开发更加轻松
    2. XML新手入门 创建构造良好的XML
    3. XML在Web应用中的优势 
    【责任编辑:彭凡 TEL:(010)68476606】