---
title: "浅解XML与DataSet对象的关系及转换"
author: "lynnlin"
date: "2010-09-30T13:39:13Z"
tags: []
categories: ["编程语言"]
slug: "xml-dataset"
draft: false
lastmod: "2010-09-30T13:39:13Z"
id: 775977
url: "https://lynnlin.iteye.com/blog/775977"
---

在.NET Framework 中，经常使用XML 作为存储和传输各种数据的格式。

DataSet 中的数据可以转换成XML 的形式来表示和存储。

我们可以使用XML 对象同步和转换DataSet 的数据，而DataSet 也可以存储和传输XML 格式的数据。

XML 与 DataSet 的关系如下图所示：

[![XmlDataDocument_DataSet](http://images.51cto.com/files/uploadimg/20100916/1441090.gif "XmlDataDocument_DataSet")](http://images.cnblogs.com/cnblogs_com/xugang/WindowsLiveWriter/XMLDataSet_E55C/XmlDataDocument_DataSet_2.gif)

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 文件如下：

```

<?xml version="1.0" encoding="UTF-8"?>   

<Persons>   


   <person>   


     <ID>0</ID>   


     <Name>Mark</Name>   


     <Age>18</Age>   


   </person>   


   <person>   


     <ID>1</ID>   


     <Name>Jorn</Name>   


     <Age>22</Age>   


   </person>   


   <person>   


     <ID>2</ID>   


     <Name>Aderson</Name>   


     <Age>30</Age>   


   </person>   


</Persons>  

```

Customer.xsd 文件如下：

```

<?xml version="1.0" encoding="UTF-8"?>   

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schema-microsoft-com:xml-msdata" elementFormDefault="qualified" attributeFormDefault="unqualified" id="Customers">   


    <xs:element name="Customers" msdata:IsDataSet="true" msdata:EnforceConstraints="False">   


        <xs:complexType>   


            <xs:choice maxOccurs="unbounded">   


                <xs:element name="Customer" type="customersType"/>   


            </xs:choice>   


        </xs:complexType>   


    </xs:element>   


    <xs:complexType name="customersType">   


        <xs:sequence>   


            <xs:element name="CustomersID" type="xs:string" minOccurs="0"/>   


            <xs:element name="CustomersName" type="xs:string" minOccurs="0"/>   


            <xs:element name="CustomersAge" type="xs:int" minOccurs="0"/>   


        </xs:sequence>   


    </xs:complexType>   


</xs:schema>  

```

Winform 程序的源代码如下：

```

namespace DataSet_XML_Demo   
{   

    public partial class Form1 : Form   

    {   

        public Form1()   

        {   
            InitializeComponent();   
        }   

        DataSet ds = new DataSet();   

    

        //读取XML文档的数据到DataSet   


        private void btnReadXML_Click(object sender, EventArgs e)   

        {   

            ds.ReadXml("http://www.cnblogs.com/" + "Person.xml");   

            dataGridView1.DataSource = ds.Tables[0];   
        }   
    

        //将DataSet中的数据写出到XML文档   


        private void btnWriteXML_Click(object sender, EventArgs e)   

        {   

            ds.WriteXml("http://www.cnblogs.com/New.xml");   


 ds.WriteXml("http://www.cnblogs.com/New_Alter.xml", XmlWriteMode.DiffGram);   

        }   
    

        //加载Schema给DataSet   


        private void btnReadXmlSchema_Click(object sender, EventArgs e)   

        {   

            DataSet newDataSet = new DataSet();   


            newDataSet.ReadXmlSchema("http://www.cnblogs.com/Customer.xsd");   

            dataGridView1.DataSource = newDataSet.Tables[0];   
        }   
    

        //将DataSet的Schema写出   


        private void btnWriteXmlSchema_Click(object sender, EventArgs e)   

        {   

            DataSet newDataSet = new DataSet();   


            DataTable dt = new DataTable();   


            DataColumn dc1 = new DataColumn("id", typeof(int));   


            DataColumn dc2 = new DataColumn("name", typeof(string));   

            dt.Columns.Add(dc1);   
            dt.Columns.Add(dc2);   
            newDataSet.Tables.Add(dt);   
    
            dataGridView1.DataSource = newDataSet;   

            dataGridView1.DataMember = "Table1";   


  newDataSet.WriteXmlSchema("http://www.cnblogs.com/newSchema.xsd");   

        }   
    

        //GetXml()方法的使用   


        private void btnGetXml_Click(object sender, EventArgs e)   

        {   

            DataSet newXml = new DataSet();   


            newXml.ReadXml("http://www.cnblogs.com/" + "Person.xml");   

            dataGridView1.DataSource = newXml.Tables[0];   
    

            //GetXml():返回DataSet中XML形式的字符串   


            string strXml = newXml.GetXml();   

            textBox1.Text = strXml;   
        }   
    

        //GetXmlSchema()方法的使用   


        private void btnGetXmlSchema_Click(object sender, EventArgs e)   

        {   

             /* 注意：   
                如果DataSet已经拥有一个Schema模式，   
                再加载新的Schema模式文件，   
                则会自动将两个Schema模式合并。   
             */   

            DataSet newSchema = new DataSet();   


      newSchema.ReadXmlSchema("http://www.cnblogs.com/Customer.xsd");   

            dataGridView1.DataSource = newSchema.Tables[0];   
    

            //GetXmlSchema():返回DataSet所使用的Schema模式文件的字符串   


            string strSchema = newSchema.GetXmlSchema();   

            textBox1.Text = strSchema;   
        }   
    }   
}  
```

Winform 程序的界面效果如下：

[![Winform 程序的界面效果图](http://images.51cto.com/files/uploadimg/20100916/1441091.png "Winform 程序的界面效果图")](http://images.cnblogs.com/cnblogs_com/xugang/WindowsLiveWriter/XMLDataSet_E55C/2010-09-12_233004_2.png)