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