对Datset导出XML到客户端的一些补充

   上一个文章对于一般的格式来说已经够钐了,可是对于一些特珠的就不行比如说,我要导出的格式是这样的
<newDataSet>
        <people id="1">
           <name>1111</name>
           <sex>1</sex>
        </people>
     </newDataSet>
     就是相应的结点要有一个属性。这个直接用Dataset输出就不行。只能进行XML重构来处理。

XmlTextWriter writer  =   new  XmlTextWriter(Response.OutputStream, Encoding.UTF8);  
   writer.Formatting 
=  Formatting.Indented;  
   writer.Indentation 
=   4 ; 
   writer.IndentChar 
=   '   ' ; 
   writer.WriteRaw(
" <?xml version=\ " 1.0 \ "  ?> " ); 
   writer.WriteStartElement(
" Yuanlin " );

   
// writer.e
    for ( int  i = 0 ;i < dt.Rows.Count;i ++ )
   
{
    writer.WriteStartElement(
"Corp");
     writer.WriteAttributeString(
"id",dt.Rows.Count.ToString());
     writer.WriteStartElement(
"CorpInfo");
      writer.WriteElementString(
"Name",dt.Rows[i]["Name"].ToString());
      writer.WriteElementString(
"AptLevel",dt.Rows[i]["AptLevel"].ToString());
      writer.WriteElementString(
"CropBirthDate",dt.Rows[i]["CropBirthDate"].ToString());
      writer.WriteElementString(
"ManageDept",dt.Rows[i]["ManageDept"].ToString());
      writer.WriteElementString(
"Province",dt.Rows[i]["Province"].ToString());
      writer.WriteElementString(
"City",dt.Rows[i]["City"].ToString());
      writer.WriteElementString(
"Region",dt.Rows[i]["Region"].ToString());
      writer.WriteElementString(
"Address",dt.Rows[i]["Address"].ToString());
     writer.WriteEndElement();
     
//获取人员信息
     DataView dv = DtEmployee.DefaultView;
     dv.RowFilter 
= String.Format("FBaseInfoID='{0}'",dt.Rows[i]["FID"].ToString());
     
     
for(int j=0;j<dv.Count;j++)
     
{
      writer.WriteStartElement(
"USerInfo");
      writer.WriteElementString(
"Name",dv[j].Row["Name"].ToString());
      writer.WriteElementString(
"Sex",dv[j].Row["Sex"].ToString());
      writer.WriteElementString(
"CardCode",dv[j].Row["CardCode"].ToString());
      writer.WriteElementString(
"Specialty",dv[j].Row["Specialty"].ToString());
      writer.WriteElementString(
"Duty",dv[j].Row["Duty"].ToString());
      writer.WriteEndElement();
     }

     dv 
= DtProject.DefaultView;
     dv.RowFilter 
= String.Format("FBaseInfoID='{0}'",dt.Rows[i]["FID"].ToString());
     
for(int j=0;j<dv.Count;j++)
     
{
      writer.WriteStartElement(
"ProjectInfo");
      writer.WriteElementString(
"FProjectName",dv[j].Row["FProjectName"].ToString());
      writer.WriteElementString(
"FProvince",dv[j].Row["FProvince"].ToString());
      writer.WriteElementString(
"FCity",dv[j].Row["FCity"].ToString());
      writer.WriteElementString(
"FRegion",dv[j].Row["FRegion"].ToString());
      writer.WriteElementString(
"FProjectType",dv[j].Row["FProjectType"].ToString());
      writer.WriteElementString(
"FArea",dv[j].Row["FArea"].ToString());
      writer.WriteElementString(
"FPrice",dv[j].Row["FPrice"].ToString());
      writer.WriteElementString(
"FCompleteArea",dv[j].Row["FCompleteArea"].ToString());
      writer.WriteElementString(
"FCompleteCost",dv[j].Row["FCompleteCost"].ToString());
      writer.WriteEndElement();
     }

     
    writer.WriteEndElement();
   }

   writer.WriteEndElement(); 
   Response.AddHeader(
" Content-Disposition " " attachment; filename=Acounts.xml " );
   
this .Response.ContentType  =   " text/xml " ;
   Response.ContentType 
=   " application/octet-stream " ;
   writer.Flush();
   Response.End();  
   writer.Close();

转载于:https://www.cnblogs.com/chen79/archive/2008/01/05/1027017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值