1、可以将DataSet中包含的DataTable对象的内容用WriteXml()方法写入XML文件中。这个方法写入的XML文件包含DataTable列名与数值。
2、可以用WriteXmlSchema()方法将DataSet对象模式写入XML文件中。用这个方法写入的XML文件包含DataSet所包含DataTable的结构。
3、可以用GetXml()方法取得DataSet中的XML,此方法返回XML字符串。
string oSql = " select id,name,station,level,dept
from verify where dept = ' 信息组 '" ;
SqlDataAdapter da = new SqlDataAdapter(oSql,con);
DataSet ds = new DataSet();
da.Fill(ds, " verify " );
con.Close();
ds.WriteXml( " c:/verify.xml " );
Response.Write( " 写入XML文档完毕 " );
XML文件内容
<? xml version = " 1.0 " standalone = " yes " ?>
< NewDataSet >
< verify >
< id > LH </ id >
< name > 李宏 </ name >
< station > 组长 </ station >
< level > 系统管理员 </ level >
< dept > 信息组 </ dept >
</ verify >
< verify >
< id > LJS </ id >
< name > 刘京生 </ name >
< station > 网络管理工程师 </ station >
< level > 普通用户 </ level >
< dept > 信息组 </ dept >
</ verify >
</ NewDataSet >
二、使用WriteXml()方法---写入包含模式的内容到XML文件
< NewDataSet >
< xs:schema id ="NewDataSet" xmlns ="" xmlns:xs ="http://www.w3.org/2001/XMLSchema" xmlns:msdata ="urn:schemas-microsoft-com:xml-msdata" >
< xs:element name ="NewDataSet" msdata:IsDataSet ="true" msdata:Locale ="zh-CN" >
< xs:complexType >
< xs:choice maxOccurs ="unbounded" >
< xs:element name ="verify" >
< xs:complexType >
< xs:sequence >
< xs:element name ="id" type ="xs:string" minOccurs ="0" />
< xs:element name ="name" type ="xs:string" minOccurs ="0" />
< xs:element name ="station" type ="xs:string" minOccurs ="0" />
< xs:element name ="level" type ="xs:string" minOccurs ="0" />
< xs:element name ="dept" type ="xs:string" minOccurs ="0" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
</ xs:choice >
</ xs:complexType >
</ xs:element >
</ xs:schema >
< verify >
< id > LH </ id >
< name > 李宏 </ name >
< station > 组长 </ station >
< level > 系统管理员 </ level >
< dept > 信息组 </ dept >
</ verify >
< verify >
< id > LJS </ id >
< name > 刘京生 </ name >
< station > 网络管理工程师 </ station >
< level > 普通用户 </ level >
< dept > 信息组 </ dept >
</ verify >
</ NewDataSet >
三、使用WriteXmlSchema()方法
CS代码
ds.WriteXmlSchema("c://verify.xml");
XML文件内容:
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="verify">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0" />
<xs:element name="name" type="xs:string" minOccurs="0" />
<xs:element name="station" type="xs:string" minOccurs="0" />
<xs:element name="level" type="xs:string" minOccurs="0" />
<xs:element name="dept" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>