国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > XML > 正文

如何用C#將數據庫中的記錄制成XML

2024-09-05 20:55:55
字體:
來源:轉載
供稿:網友
以前在一個公司項目中要用數據庫中的記錄生成相應的xml文件[主要是為了提高訪問速度],但由于當時資料的缺乏,在開發過程中遇到了不過的困難,好在最終完成了工作,我在這里把當時其中的一個功能函數列出來,其于的函數大同小意,希望兄弟們以后在遇到這樣的問題時不象我當初一樣再吃苦頭.
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: 該調用是 asp.net web 服務設計器所必需的
initializecomponent();
}

#region 組件設計器生成的代碼

//web 服務設計器所必需的
private icontainer components = null;

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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());//按時期生成相應的時期型文件夾
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("按在數據庫中記錄的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) //依次找出當前記錄的所有列屬性
{
if ((c.caption.tostring()!="pic"))
{
picxmlwriter.writestartelement("",c.caption.tostring().trim(),""); //寫入字段名
picxmlwriter.writestring(r[c].tostring().trim()); //寫入數據
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()!="") //數據庫中圖片字段的插入格式為: 文件名,高,寬| 以此類推. 例如 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();
//關閉編寫器
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);
}

}





發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临潭县| 独山县| 虎林市| 孟津县| 财经| 五原县| 东兴市| 宁海县| 娱乐| 娄烦县| 丘北县| 承德县| 海盐县| 杭锦旗| 鄂温| 交城县| 平武县| 隆德县| 永寿县| 海阳市| 湖州市| 雷山县| 东城区| 武汉市| 鄂托克旗| 桂阳县| 且末县| 苏尼特左旗| 新平| 侯马市| 隆化县| 石嘴山市| 会宁县| 霍林郭勒市| 龙海市| 长白| 白城市| 商河县| 临汾市| 元氏县| 阳城县|