using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace CreateHtmlBLL
{
public class CreateHtmlBLL
{
#region##读取配置文件某节点的个数
///
/// 读取配置文件某节点的个数
///
///配置文件的路径
///要获取的节点
///返回节点个数
private int ReadConfig(string path,string nodeName)
{
string absoPath = string.Empty; //绝对路径
try
{
absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
XmlDocument xd = new XmlDocument();
xd.Load(absoPath);
XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相应节点的集合
return nodeList.Count;
}
catch (Exception)
{
throw;
}
}
#endregion
#region##创建文件夹
///
/// 创建文件夹
///
///要创建的路径
public void CreatFolder(string path)
{
string absoPath = string.Empty; //绝对路径
try
{
absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
if (!Directory.Exists(absoPath))
{
Directory.CreateDirectory(absoPath);
}
}
catch (Exception)
{
throw;
}
}
#endregion
#region##生成HTML页
///
/// 生成HTML页
///
///配置文件的路径
///配置文件节点名
///模版页路径
///替换数组
///生成HTML路径
public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath)
{
string fileName = string.Empty; //生成文件名
string absoCrePath = string.Empty; //生成页绝对路径
string absoTemPath = string.Empty; //模版页的绝对中径
int nodeCount = 0; //节点数
try
{
absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath);
absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath);
nodeCount = ReadConfig(configPath, configNodeName);
FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read); //读取模版页
StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
StringBuilder sb = new StringBuilder(sr.ReadToEnd());
sr.Close();
sr.Dispose();
for (int i = 0; i < nodeCount; i++)
{
sb.Replace("$Porschev[" + i + "]$", arr[i]);
}
CreatFolder(createPath);
fileName = DateTime.Now.ToFileTime().ToString() + ".html"; //设置文件名(这里可以根据需要变化命名)
FileStream cfs = File.Create(absoCrePath + "/" + fileName);
StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8"));
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
sw.Dispose();
}
catch (Exception)
{
throw;
}
}
#endregion
}
}
|