以前在一個公司項目中要用數(shù)據(jù)庫中的記錄生成相應(yīng)的xml文件[主要是為了提高訪問速度],但由于當(dāng)時資料的缺乏,在開發(fā)過程中遇到了不過的困難,好在最終完成了工作,我在這里把當(dāng)時其中的一個功能函數(shù)列出來,其于的函數(shù)大同小意,希望兄弟們以后在遇到這樣的問題時不象我當(dāng)初一樣再吃苦頭.
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.web;
using system.web.services;
using system.xml;
using system.data.sqlclient;
using system.configuration;
using system.text;
using system.xml.xsl;
using system.io;
namespace admin
{
/// <summary>
/// createxml 的摘要說明。
/// </summary>
///
[system.web.services.webservice(namespace="http://..../admin/createxml.asmx",description="生成或更新星迷俱樂部中的xml文件")]
public class createxml : system.web.services.webservice
{
public createxml()
{
//codegen: 該調(diào)用是 asp.net web 服務(wù)設(shè)計器所必需的
initializecomponent();
}
#region 組件設(shè)計器生成的代碼
//web 服務(wù)設(shè)計器所必需的
private icontainer components = null;
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
{
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if(disposing && components != null)
{
components.dispose();
}
base.dispose(disposing);
}
#endregion
[webmethod]
public string createclubxmlbyid(string id)
{
datetime filenamedate=datetime.now;
createpath("..//"+filenamedate.year.tostring(),filenamedate.month.tostring()+"_"+filenamedate.day.tostring());//按時期生成相應(yīng)的時期型文件夾
string filename=server.mappath("..//"+filenamedate.year.tostring()+"//"+filenamedate.month.tostring()+"_"+filenamedate.day.tostring()+"//club"+id.trim()+".xml");
xmltextwriter picxmlwriter = null;
encoding gb = encoding.getencoding("gb2312");
picxmlwriter = new xmltextwriter (filename,gb);
try
{
string strconn=configurationsettings.appsettings["starclub"];
string sqlstatement="select * from club where id="+id.tostring().trim();
sqlconnection myconnection= new sqlconnection(strconn);
sqldataadapter mycommand = new sqldataadapter(sqlstatement,myconnection);
dataset mydataset;
mycommand.selectcommand.commandtype=commandtype.text;
mydataset = new dataset();
mycommand.fill(mydataset, "mytable");
picxmlwriter.formatting = formatting.indented;
picxmlwriter.indentation= 6;
picxmlwriter.namespaces = false;
picxmlwriter.writestartdocument();
//picxmlwriter.writedoctype("文檔類型", null, ".xml", null);
//picxmlwriter.writecomment("按在數(shù)據(jù)庫中記錄的id進行記錄讀寫");
picxmlwriter.writeprocessinginstruction("xml-stylesheet","type='text/xsl' href='../../xsl/1.xsl'") ; //寫入用于解釋的xsl文件名
picxmlwriter.writestartelement("","club","");
foreach(datarow r in mydataset.tables[0].rows) //依次取出所有行
{
picxmlwriter.writestartelement("","record","");
foreach(datacolumn c in mydataset.tables[0].columns) //依次找出當(dāng)前記錄的所有列屬性
{
if ((c.caption.tostring()!="pic"))
{
picxmlwriter.writestartelement("",c.caption.tostring().trim(),""); //寫入字段名
picxmlwriter.writestring(r[c].tostring().trim()); //寫入數(shù)據(jù)
picxmlwriter.writeendelement();
}
else
{
picxmlwriter.writestartelement("",c.caption.tostring().trim(),"");
string [] pic=r[c].tostring().trim().split('|');
for (int i=0;i<pic.length;i++)
{
if (pic[i].trim()!="") //數(shù)據(jù)庫中圖片字段的插入格式為: 文件名,高,寬| 以此類推. 例如 no.jpg,132,142|
{
picxmlwriter.writestartelement("",c.caption.tostring().trim()+"s","");
string [] picstr=pic[i].split(',');
picxmlwriter.writestartelement("","picstr","");
picxmlwriter.writestring(picstr[0].trim().trim());
picxmlwriter.writeendelement();
picxmlwriter.writestartelement("","height","");
picxmlwriter.writestring(picstr[1].trim().trim());
picxmlwriter.writeendelement();
picxmlwriter.writestartelement("","width","");
picxmlwriter.writestring(picstr[1].trim().trim());
picxmlwriter.writeendelement();
picxmlwriter.writestartelement("","comment","");
picxmlwriter.writestring(pic[++i].trim().trim());
picxmlwriter.writeendelement();
picxmlwriter.writeendelement();
}
else
{
i++;
}
}
picxmlwriter.writeendelement();
}
}
picxmlwriter.writeendelement();
}
picxmlwriter.writeendelement();
picxmlwriter.flush();
}
catch (exception e)
{
console.writeline ("異常:{0}", e.tostring());
}
finally
{
console.writeline();
console.writeline("對文件 {0} 的處理已完成。", id);
if (picxmlwriter != null)
picxmlwriter.close();
//關(guān)閉編寫器
if (picxmlwriter != null)
picxmlwriter.close();
}
return filenamedate.year.tostring()+"//"+filenamedate.month.tostring()+"_"+filenamedate.day.tostring()+"//club"+id.trim()+".xml";
}
public void createpath(string yearpath,string monthdaycurrent)
{
string path=server.mappath("");
if (directory.exists(path+yearpath))
{
if (directory.exists(path+yearpath+monthdaycurrent))
{
;
}
else
{
directory.createdirectory(path+"//"+yearpath+"//"+monthdaycurrent);
}
}
else
{
directory.createdirectory(path+"//"+yearpath+"//"+monthdaycurrent);
}
}