方法1:
// 创建MemeoryStream
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// 写dataset到MemeoryStream
ds.WriteXml(ms,XmlWriteMode.IgnoreSchema);
Response.Clear();
// 下载附件的名字
Response.AddHeader("Content-Disposition", "attachment; filename=Acounts.xml");
// 下载附件的大小,以便让浏览器显示进度条
Response.AddHeader("Content-Length", ms.Length.ToString());
// 指定浏览器为下载模式
Response.ContentType = "application/octet-stream";
// 发送到客户端
byte[] b = ms.ToArray();
Response.OutputStream.Write(b,0,b.Length);
Response.End();
方法2:
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
writer.WriteRaw("<?xml version=\"1.0\" ?>");
ds.WriteXml(writer);
Response.AddHeader("Content-Disposition", "attachment; filename=Acounts.xml");
this.Response.ContentType = "text/xml";
Response.ContentType = "application/octet-stream";
writer.Flush();
Response.End();
writer.Close();
方法3:自定义一个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();