欢迎光临
我们一直在努力

如何用C#将数据库中的记录制成XML-.NET教程,C#语言

建站超值云服务器,限时71元/月

以前在一个公司项目中要用数据库中的记录生成相应的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);

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 如何用C#将数据库中的记录制成XML-.NET教程,C#语言
分享到: 更多 (0)

相关推荐

  • 暂无文章