明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

如何用C#將數(shù)據(jù)庫中的記錄制成XML

[摘要]以前在一個(gè)公司項(xiàng)目中要用數(shù)據(jù)庫中的記錄生成相應(yīng)的XML文件[主要是為了提高訪問速度],但由于當(dāng)時(shí)資料的缺乏,在開發(fā)過程中遇到了不過的困難,好在最終完成了工作,我在這里把當(dāng)時(shí)其中的一個(gè)功能函數(shù)列出來,其于的函數(shù)大同小意,希望兄弟們以后在遇到這樣的問題時(shí)不象我當(dāng)初一樣再吃苦頭.using System...

    以前在一個(gè)公司項(xiàng)目中要用數(shù)據(jù)庫中的記錄生成相應(yīng)的XML文件[主要是為了提高訪問速度],但由于當(dāng)時(shí)資料的缺乏,在開發(fā)過程中遇到了不過的困難,好在最終完成了工作,我在這里把當(dāng)時(shí)其中的一個(gè)功能函數(shù)列出來,其于的函數(shù)大同小意,希望兄弟們以后在遇到這樣的問題時(shí)不象我當(dāng)初一樣再吃苦頭.
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: 該調(diào)用是 ASP.NET Web 服務(wù)設(shè)計(jì)器所必需的
   InitializeComponent();
  }

  #region 組件設(shè)計(jì)器生成的代碼
 
  //Web 服務(wù)設(shè)計(jì)器所必需的
  private IContainer components = null;
   
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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());//按時(shí)期生成相應(yīng)的時(shí)期型文件夾
   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("按在數(shù)據(jù)庫中記錄的ID進(jìn)行記錄讀寫");
    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)  //依次找出當(dāng)前記錄的所有列屬性
     {
      if ((c.Caption.ToString()!="pic"))
      {
       PicXmlWriter.WriteStartElement("",c.Caption.ToString().Trim(),"");  //寫入字段名
       PicXmlWriter.WriteString(r[c].ToString().Trim());   //寫入數(shù)據(jù)
       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()!="")   //數(shù)據(jù)庫中圖片字段的插入格式為:  文件名,高,寬   以此類推. 例如  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("對(duì)文件 {0} 的處理已完成。", ID);
    if (PicXmlWriter != null)
     PicXmlWriter.Close();
    //關(guān)閉編寫器
    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);
   }

  }