mysql遍历 xml文件路径_遍历指定文件夹下所有的xml文件并动态生成HTML页面!

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Xml;

using System.Net;

using System.Text;

using System.Text.RegularExpressions;

namespace AspNet

{

///

/// 遍历指定文件夹下所有的Data.xml文件并动态生成HTML静态页面!

/// 运行过程:[1调用2,2调用3,3调用4]

///

public class GetPageHtml : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button WebClientButton;

protected System.Web.UI.WebControls.TextBox ContentHtml;

protected System.Web.UI.WebControls.Button GetText;

protected System.Web.UI.WebControls.TextBox UrlText;

protected System.Web.UI.WebControls.Button Button2;

protected System.Web.UI.HtmlControls.HtmlInputFile FilePath;

private string PageUrl = "";

private void Page_Load(object sender, System.EventArgs e)

{

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

///

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

///

private void InitializeComponent()

{

this.WebClientButton.Click += new System.EventHandler(this.WebClientButton_Click);

this.Button2.Click += new System.EventHandler(this.Button2_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

///

/// 获取HTML代码

///

///

///

private void WebClientButton_Click(object sender, System.EventArgs e)

{

PageUrl = UrlText.Text.Trim();

WebClient wc = new WebClient();

wc.Credentials = CredentialCache.DefaultCredentials;

Byte[] pageData = wc.DownloadData(PageUrl);

ContentHtml.Text = Encoding.Default.GetString(pageData);

wc.Dispose();

}

///

/// 方法1:调用方法2 BianLi 去遍历文件

///

///

///

private void Button2_Click(object sender, System.EventArgs e)

{

string Dir=FilePath.Value.Trim();

string str="";

if (Dir=="")

{

Response.Write("");

return;

}

else

{

//Response.Write(Dir.Replace(Dir.Remove(0,Dir.LastIndexOf(@"\")),"")+@"\");

str=Dir.Replace(Dir.Remove(0,Dir.LastIndexOf(@"\")),"")+@"\";

//调用遍历文件夹的方法BianLi(path)

BianLi(str);

Response.Write("");

}

}

///

/// 方法2:遍历出指定文件夹下的所有文件,并调用方法3:ReadXmlData(Dir)读取XML数据

///

///

private void BianLi(string path)

{

string[] fileNames = Directory.GetFiles(path);

string[] directories = Directory.GetDirectories(path);

string Dir="";

foreach (string file in fileNames)

{

//如果路径信息中包含Data.xml文件则输出有用的路径

if(file.Remove(0,file.LastIndexOf(@"\")).Replace(@"\","")=="Data.xml")

Dir=file.Remove(0,file.IndexOf(@"car\")).Replace("Data.xml","").Replace(@"\GetPageHtml\","").Replace(@"\",@"/").Trim();

//调用ReadXmlData方法去读取Data.xml信息^_^

if(Dir!="")  ReadXmlData(Dir);

//Response.Write(Dir+"
");

}

foreach (string dir in directories)

{

//再次遍历

BianLi(dir);

}

}

///

/// 方法3:ReadXmlData,读取xml信息,调用方法4去生成HTML页面

///

private void ReadXmlData(string filepath)

{

string FileName = Server.MapPath(@filepath+"Data.xml");

if ( ! System.IO.File.Exists(FileName))

{

// 输出文件不存在错误信息

Response.Write("抱歉,Data.xml文件不存在");

return;

}

XmlDocument doc = new XmlDocument();

try

{

doc.Load(FileName);

//输出指定的一个结点:

//XmlNode node = doc.SelectSingleNode("//main[@id='1']");

//输出所有的结点^_^

XmlNode node = doc.SelectSingleNode("//Root");

if (node != null)

{

int i=0;

int j=1;

string str="";

foreach (XmlElement E in node.ChildNodes)

{

foreach (XmlElement F in E.ChildNodes)

{

//先用_组合字符

str=str+F.OuterXml+"_";

str=str.Trim();

i=i+1;

if(i %  3==0)

{

string delimStr = "_";

char [] delimiter = delimStr.ToCharArray();

string [] split =str.Split(delimiter);

//利用撤分的变量来调用生成静态页面的方法:

CreateHtml(j,split[0],split[1],split[2],@filepath);

if (i==1) Response.Write(@filepath+"
");

str="";

j=j+1;

}

}

}

}

}

catch

{

// 输出错误信息:xml格式不正确

// Response.Write("Data.xml格式不正确");

// Response.Write("错误文件路径信息:"+@filepath+"
");

return;

}

}

///

///  方法4:CreateHtml,根据读取的xml信息循环生成HTM静态页面

///

///

///

///

///

private void CreateHtml(int id,string Car_Jpg,string Car_Title,string Car_Content,string filepath )

{

//定义HTML模板

string content = ContentHtml.Text;

//定义生成的HTM文件名

string FileName = @filepath+id + ".htm";

//判断ID.HTM是否存在^_^,存在立即删除

if (File.Exists(Server.MapPath(".") + Path.DirectorySeparatorChar +FileName))

File.Delete(Server.MapPath(".") + Path.DirectorySeparatorChar +FileName);

FileStream fs = new FileStream(Server.MapPath(".") + Path.DirectorySeparatorChar + FileName, FileMode.CreateNew, FileAccess.Write, FileShare.None);

StreamWriter sr = new StreamWriter(fs,System.Text.Encoding.GetEncoding("Gb2312"));

//替换图片

content = content.Replace("Car_Jpg",Car_Jpg);

//替换标题

content = content.Replace("Car_Title",Car_Title);

//替换内容

content = content.Replace("Car_Content",Car_Content);

//替换内容

content = content.Replace("Car_ID",id.ToString());

//替换HTM代码中的多余字符:

content = content.Replace("","");

content = content.Replace("","");

//到这里才生成HTML静态页面^_^,不过挺舒服的拉^_^

sr.WriteLine(content);

sr.Close();

sr = null;

}

}

}

+++++++++++++++++++++++++Data.xml

1.jpg

title

content

2.jpg

title

content

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值