如何用C#將資料庫中的記錄製成XML

來源:互聯網
上載者:User
xml|資料|資料庫 以前在一個公司項目中要用資料庫中的記錄產生相應的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);
}

}





相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。