zoukankan      html  css  js  c++  java
  • XML 与DataSet 对象的关系

     

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

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

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

     

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

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

    <?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 程序的界面效果图

    源码下载

     

    作者: XuGang   网名:钢钢
    出处: http://xugang.cnblogs.com
    声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址!
  • 相关阅读:
    light_oj 1197 区间素数筛
    light_oj 1213
    light_oj 1220 素数分解
    bestcoder#43 1002 在数组中找两个数的和取模的最大值 二分
    6月7日 bc总结
    省赛总结
    二分图最大点权独立集 二分图最大点权覆盖
    终于能理解kmp算法了
    hduoj2087 统计串t在串s中出现的次数,重叠不算 KMP
    带权二分图的最大权匹配 KM算法模版
  • 原文地址:https://www.cnblogs.com/xugang/p/1827988.html
Copyright © 2011-2022 走看看