温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

XML与DataSet对象的关系是什么

发布时间:2021-07-20 14:08:53 来源:亿速云 阅读:162 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关XML与DataSet对象的关系是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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

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

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

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

XML与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 程序的界面效果如下:

XML与DataSet对象的关系是什么

以上就是XML与DataSet对象的关系是什么,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI