将DataSet对象中DataTable对象的数据写入XML文件

XML是移动信息的方便格式。
写入方法:
1、可以将DataSet中包含的DataTable对象的内容用WriteXml()方法写入XML文件中。这个方法写入的XML文件包含DataTable列名与数值。
2、可以用WriteXmlSchema()方法将DataSet对象模式写入XML文件中。用这个方法写入的XML文件包含DataSet所包含DataTable的结构。
3、可以用GetXml()方法取得DataSet中的XML,此方法返回XML字符串。
一、使用WriteXml()方法—写入内容到XML文件
CS代码
con.Open();
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文件
System.Data.XmlWriteMode枚举常量可以指定如何写出XML数据与模式,常量说明如下:
CS代码
ds.WriteXml("c://verify.xml",XmlWriteMode.WriteSchema);//在写入XML文件 中使用XmlWriteMode.WriteSchema常量
<? xml version="1.0" standalone="yes" ?>
< 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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值