程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 如何用C#將數據庫中的記錄制成XML

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

編輯:.NET實例教程

以前在一個公司項目中要用數據庫中的記錄生成相應的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);
}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved