标题:[转帖]用C#将数据库中的记录制成XML
只看楼主
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
结帖率:66.67%
 问题点数:0 回复次数:2 
[转帖]用C#将数据库中的记录制成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()+"file://"+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()+"file://"+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+"file://"+yearpath+"//"+monthdaycurrent); } } else { Directory.CreateDirectory(path+"file://"+yearpath+"//"+monthdaycurrent); }

}

搜索更多相关主题的帖子: using System 数据库 XML 转帖 
2005-05-15 08:53
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
得分:0 
估计能看懂的不多

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-05-16 10:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-18244-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.119290 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved