using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPackDemo.Comm;
namespace HtmlAgilityPackDemo
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BXHTML b = new BXHTML();
foreach (part pp in b.p)
{
foreach (DataItem di in pp.items)
{
}
}
Response.Write(b.Build());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using HtmlAgilityPackDemo.Comm;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
namespace HtmlAgilityPackDemo.Comm
{
public class BXHTML
{
string dd = string.Empty;
string firstpage = "index.html";
string homepage = "首页";
HtmlDocument doc = new HtmlDocument();
string orgHTML = string.Empty;
public List<part> p = new List<part>();
public List<SingleDataItem> sdi = new List<SingleDataItem>();
int pindex = 1;
int CotIndex = 1;
int ItemID = 1;
string MenuOutHtml = string.Empty;
string ResultMenuOutHtml = string.Empty;
public string Build()
{
//模板页 手工设置分界点
#region 首页
string apppath = AppDomain.CurrentDomain.BaseDirectory + @"bin\m\";
apppath = AppDomain.CurrentDomain.BaseDirectory + @"bin\m3\html\";
if (!File.Exists(apppath + firstpage))
{
firstpage = "index.htm";
if (!File.Exists(apppath + firstpage))
{
//手要设置
}
}
#endregion
#region 菜单
//orgHTML = File.ReadAllText(apppath + firstpage, EncodingType.GetType(apppath + firstpage));
doc.Load(apppath + firstpage, EncodingType.GetType(apppath + firstpage));
HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a[@href]");
HtmlNode Home = null;
foreach (HtmlNode item in links)
{
if ((item.InnerText.Contains("首页") || item.InnerText.Contains("home")) && (item.Attributes["href"].Value == firstpage))
{
Home = item;
break;
}
}
if (Home != null)
{
Home = Home.ParentNode.ParentNode;
MenuOutHtml = Home.OuterHtml;
List<linkhref> Menus = GetMenus(Home.OuterHtml);
}
else
{
//手工设置
}
#endregion
#region 版块内容
//中心
//div ul li a
//div dl dt dd
//div ol li a
//由内向外
HtmlDocument bandoc = new HtmlDocument();
bandoc.Load(apppath + firstpage, EncodingType.GetType(apppath + firstpage));
orgHTML = bandoc.DocumentNode.OuterHtml;
//移除菜单
orgHTML = orgHTML.Replace(MenuOutHtml, "@@@@Menu");
bandoc = new HtmlDocument();
bandoc.LoadHtml(orgHTML);
Dg(bandoc.DocumentNode);
#endregion
#region 单一的链接 文本 图片
bandoc = new HtmlDocument();
bandoc.LoadHtml(orgHTML);
//清除单个的链接 图片 文本
string rs = R(bandoc);
orgHTML = orgHTML.Replace(bandoc.DocumentNode.OuterHtml, rs);
#endregion
#region 菜单
part ppmenu = new part();
ppmenu.PartIndex = 999;
ppmenu.Html = MenuOutHtml;
innerIn(ppmenu, "li");
ResultMenuOutHtml = ppmenu.ResultHtml;
orgHTML = orgHTML.Replace("@@@@Menu", ResultMenuOutHtml);
#endregion
#region 版块内容
foreach (part pp in p)
{
orgHTML = orgHTML.Replace(pp.TagName, pp.ResultHtml);
}
#endregion
return orgHTML;
}
private string R(HtmlDocument bandoc)
{
string Rhtml = bandoc.DocumentNode.OuterHtml;
HtmlDocument Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
string Old = string.Empty;
HtmlNodeCollection als = Rbandoc.DocumentNode.SelectNodes("//a");
if (als != null && als.Count > 0)
{
foreach (HtmlNode item1 in als)
{
//只是javavoid(0)不处理
if (!string.IsNullOrWhiteSpace(item1.InnerHtml.Trim()))
{
Old = item1.OuterHtml;
SingleDataItem sdd=new SingleDataItem();
sdd.ID = CotIndex;
sdd.URL = item1.GetAttributeValue("href", "");
sdd.Name = item1.GetAttributeValue("text", "");
if (item1.InnerHtml == item1.InnerText)
{
sdd.Name = item1.InnerHtml;
}
sdi.Add(sdd);
if (!string.IsNullOrWhiteSpace(item1.GetAttributeValue("href", "")))
Old = Old.Replace(item1.GetAttributeValue("href", ""), "<%=GetUrl(" + CotIndex.ToString() + ") %>");
if (!string.IsNullOrWhiteSpace(item1.GetAttributeValue("text", "")))
Old = Old.Replace(item1.GetAttributeValue("text", ""), "<%=GetTitle(" + CotIndex.ToString() + ") %>");
if (item1.InnerHtml == item1.InnerText)
{
Old = Old.Replace(item1.InnerHtml, "<%=GetTitle(" + CotIndex.ToString() + ") %>");
}
Rhtml = Rhtml.Replace(item1.OuterHtml.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim(), Old);
CotIndex++;
}
}
}
Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
HtmlNodeCollection imgs = Rbandoc.DocumentNode.SelectNodes("//img");
if (imgs != null && imgs.Count > 0)
{
foreach (HtmlNode item1 in imgs)
{
Old = item1.OuterHtml;
SingleDataItem sdd = new SingleDataItem();
sdd.ID = CotIndex;
sdd.imagePath = item1.GetAttributeValue("src", "");
sdd.Name = item1.GetAttributeValue("alt", "");
sdi.Add(sdd);
if (!string.IsNullOrWhiteSpace(item1.GetAttributeValue("src", "")))
Old = Old.Replace(item1.GetAttributeValue("src", ""), "<%=GetImagePath(" + CotIndex.ToString() + ") %>");
if (!string.IsNullOrWhiteSpace(item1.GetAttributeValue("alt", "")))
Old = Old.Replace(item1.GetAttributeValue("alt", ""), "<%=GetTitle(" + CotIndex.ToString() + ") %>");
Rhtml = Rhtml.Replace(item1.OuterHtml.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim(), Old);
CotIndex++;
}
}
//清除所有的html非内容标记
string Texthtml = string.Empty;
Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
foreach (var script in Rbandoc.DocumentNode.Descendants("script").ToArray())
script.Remove();
foreach (var style in Rbandoc.DocumentNode.Descendants("style").ToArray())
style.Remove();
//Texthtml = Regex.Replace(Rbandoc.DocumentNode.OuterHtml, @"<(.[^>]*)>", "\n", RegexOptions.IgnoreCase);
Texthtml = Rbandoc.DocumentNode.OuterHtml;
Texthtml = Texthtml.Replace("<", "\n<");
Texthtml = Texthtml.Replace(">", ">\n");
Texthtml = Texthtml.Replace("\r", "");
Texthtml = Texthtml.Replace("\t", "");
//string text = Rbandoc.DocumentNode.InnerText;
//Rbandoc = new HtmlDocument();
//Rbandoc.LoadHtml(Rhtml);
string[] texts = Texthtml.Split('\n');
//string itemrr = string.Empty;
foreach (string textt in texts)
{
//itemrr = NoHTML(textt);
if (textt != "--" && textt != "-" && textt != null && !string.IsNullOrWhiteSpace(textt.Trim()) && !textt.Contains("@@@@") && !textt.Contains("<") && !textt.Contains(">"))
{
SingleDataItem sdd = new SingleDataItem();
sdd.ID = CotIndex;
sdd.Name = textt.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
sdi.Add(sdd);
Rhtml = Rhtml.Replace(textt.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim(), "<%=GetTitle(" + CotIndex.ToString() + ") %>");
CotIndex++;
}
}
return Rhtml;
}
private void Dg(HtmlNode boot)
{
HtmlNodeCollection subs = boot.ChildNodes;
foreach (HtmlNode sub in subs)
{
if (sub.InnerHtml.Contains("</ul>") || sub.InnerHtml.Contains("</dl>") || sub.InnerHtml.Contains("</ol>"))
{
Dg(sub);
}
else
{
switch (sub.Name)
{
case "ul":
{
if (sub.SelectNodes("li") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "li");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
else if (sub.SelectNodes("a") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "a");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
}
break;
case "dl":
{
if (sub.SelectNodes("dt") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "dt");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
else if (sub.SelectNodes("dd") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "dd");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
else if (sub.SelectNodes("a") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "a");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
}
break;
case "ol":
{
if (sub.SelectNodes("li") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "li");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
else if (sub.SelectNodes("a") != null)
{
part pp = new part();
pp.TagName = "@@@@" + pindex.ToString();
pp.Html = sub.OuterHtml;
pp.PartIndex = pindex;
innerIn(pp, "a");
p.Add(pp);
orgHTML = orgHTML.Replace(pp.Html, pp.TagName);
pindex++;
}
}
break;
default:
Dg(sub);
break;
}
}
}
}
private DataItem GetItem(HtmlNode item1, int PartIndex)
{
int i = 1;
DataItem item = new DataItem();
item.ID = ItemID;
string Rhtml = item1.OuterHtml;
HtmlDocument Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
Rhtml = DataText(Rhtml, i, item);
Rbandoc.LoadHtml(Rhtml);
HtmlNodeCollection als = Rbandoc.DocumentNode.SelectNodes("//a");
if (als != null && als.Count > 0)
{
foreach (HtmlNode item2 in als)
{
//只是javavoid(0)不处理
if (!string.IsNullOrWhiteSpace(item1.InnerHtml.Trim()))
{
item.URL = item1.GetAttributeValue("href", "");
item.Name = item1.GetAttributeValue("text", "");
if (item1.InnerHtml == item1.InnerText)
{
item.Name = item1.InnerHtml;
}
else
{
}
}
}
}
Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
HtmlNodeCollection imgs = Rbandoc.DocumentNode.SelectNodes("//img");
if (imgs != null && imgs.Count > 0)
{
foreach (HtmlNode item3 in imgs)
{
item.imagePath = item3.GetAttributeValue("src", "");
item.Name = item3.GetAttributeValue("alt", "");
}
}
return item;
}
private string DataText(string Rhtml, int ItemIndex, DataItem item)
{
string Texthtml = string.Empty;
HtmlDocument Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
foreach (var script in Rbandoc.DocumentNode.Descendants("script").ToArray())
script.Remove();
foreach (var style in Rbandoc.DocumentNode.Descendants("style").ToArray())
style.Remove();
//Texthtml = Regex.Replace(Rbandoc.DocumentNode.OuterHtml, @"<(.[^>]*)>", "\n", RegexOptions.IgnoreCase);
Texthtml = Rbandoc.DocumentNode.OuterHtml;
Texthtml = Texthtml.Replace("<", "\n<");
Texthtml = Texthtml.Replace(">", ">\n");
Texthtml = Texthtml.Replace("\r", "");
Texthtml = Texthtml.Replace("\t", "");
//string text = Rbandoc.DocumentNode.InnerText;
//Rbandoc = new HtmlDocument();
//Rbandoc.LoadHtml(Rhtml);
string[] texts = Texthtml.Split('\n');
//string itemrr = string.Empty;
int i = ItemIndex;
foreach (string textt in texts)
{
//itemrr = NoHTML(textt);
if (textt != "--" && textt != "-" && textt != null && !string.IsNullOrWhiteSpace(textt.Trim()) && !textt.Contains("@@@@") && !textt.Contains("<") && !textt.Contains(">"))
{
switch (i)
{
case 1:
item.E001 = textt.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
break;
case 2:
item.E002 = textt.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
break;
}
i++;
}
}
return Rhtml;
}
private string ItemTemplate(string ItemTemplateStr)
{
int i = 1;
string Rhtml = ItemTemplateStr;
HtmlDocument Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
string Old = string.Empty;
Rhtml = ClearText(Rhtml, i);
Rbandoc.LoadHtml(Rhtml);
HtmlNodeCollection als = Rbandoc.DocumentNode.SelectNodes("//a");
if (als != null && als.Count > 0)
{
foreach (HtmlNode item1 in als)
{
//只是javavoid(0)不处理
if (!string.IsNullOrWhiteSpace(item1.InnerHtml.Trim()))
{
Old = item1.OuterHtml;
item1.SetAttributeValue("href", "<%# Eval(\"id\") %>");
item1.SetAttributeValue("text", "<%# Eval(\"name\") %>");
if (item1.InnerHtml == item1.InnerText)
{
item1.InnerHtml = "<%# Eval(\"name\") %>";
}
else
{
}
Rhtml = Rhtml.Replace(Old, item1.OuterHtml);
}
}
}
Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
HtmlNodeCollection imgs = Rbandoc.DocumentNode.SelectNodes("//img");
if (imgs != null && imgs.Count > 0)
{
foreach (HtmlNode item3 in imgs)
{
Old = item3.OuterHtml;
item3.SetAttributeValue("src", "<%# Eval(\"id\") %>");
item3.SetAttributeValue("alt", "<%# Eval(\"name\") %>");
Rhtml = Rhtml.Replace(Old, item3.OuterHtml);
}
}
//清除所有的html非内容标记
//ClearText(Rhtml, i);
return Rhtml;
}
private string ClearText(string Rhtml, int ItemIndex)
{
string Texthtml = string.Empty;
HtmlDocument Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
foreach (var script in Rbandoc.DocumentNode.Descendants("script").ToArray())
script.Remove();
foreach (var style in Rbandoc.DocumentNode.Descendants("style").ToArray())
style.Remove();
//Texthtml = Regex.Replace(Rbandoc.DocumentNode.OuterHtml, @"<(.[^>]*)>", "\n", RegexOptions.IgnoreCase);
Texthtml = Rbandoc.DocumentNode.OuterHtml;
Texthtml = Texthtml.Replace("<", "\n<");
Texthtml = Texthtml.Replace(">", ">\n");
Texthtml = Texthtml.Replace("\r", "");
Texthtml = Texthtml.Replace("\t", "");
//string text = Rbandoc.DocumentNode.InnerText;
Rbandoc = new HtmlDocument();
Rbandoc.LoadHtml(Rhtml);
string[] texts = Texthtml.Split('\n');
//string itemrr = string.Empty;
int i = ItemIndex;
foreach (string textt in texts)
{
//itemrr = NoHTML(textt);
if (textt != "--" && textt != "-" && textt != null && !string.IsNullOrWhiteSpace(textt.Trim()) && !textt.Contains("@@@@") && !textt.Contains("<") && !textt.Contains(">"))
{
Rhtml = Rhtml.Replace(textt.Replace("\t", string.Empty).Replace("\r", string.Empty).Trim(), "<%# Eval(\"E" + i.ToString().PadLeft(3, '0') + "\") %>");
i++;
}
}
return Rhtml;
}
private string NoHTML(string Htmlstring) //替换HTML标记
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<img[^>]*>;", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
private void innerIn(part pp, string tag)
{
HtmlDocument bandoc = new HtmlDocument();
bandoc.LoadHtml(pp.Html);
HtmlNodeCollection lis = bandoc.DocumentNode.SelectNodes("//" + tag);
if (lis != null && lis.Count > 0)
{
if (lis.Count > 1)
{
pp.ResultHtml = lis[1].ParentNode.OuterHtml.Replace(lis[1].ParentNode.InnerHtml, "<asp:Repeater ID=\"Repeater" + pp.PartIndex.ToString() + "\" runat=\"server\"><ItemTemplate>" + ItemTemplate(lis[1].OuterHtml) + "</ItemTemplate></asp:Repeater>");
}
else
{
pp.ResultHtml = lis[0].ParentNode.OuterHtml.Replace(lis[0].ParentNode.InnerHtml, "<asp:Repeater ID=\"Repeater" + pp.PartIndex.ToString() + "\" runat=\"server\"><ItemTemplate>" + ItemTemplate(lis[0].OuterHtml) + "</ItemTemplate></asp:Repeater>");
}
pp.items = new List<DataItem>();
foreach (HtmlNode item1 in lis)
{
pp.items.Add(GetItem(item1, pp.PartIndex));
ItemID++;
}
}
}
private List<linkhref> GetMenus(string OuterHtml)
{
List<linkhref> hrefs = null;
HtmlDocument doc = GetDoc(OuterHtml);
HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a[@href]");
if (links != null)
{
hrefs = new List<linkhref>();
linkhref href = new linkhref();
foreach (HtmlNode item in links)
{
href = new linkhref();
href.Name = item.InnerText;
href.Href = item.Attributes["href"].Value;
//字段
if ((item.InnerText.Contains("首页") || item.InnerText.Contains("home")) && (item.Attributes["href"].Value == firstpage))
{
href.IsFirst = true;
}
//没有地址的能判定为
if (!string.IsNullOrWhiteSpace(href.Href))
{
hrefs.Add(href);
}
}
}
return hrefs;
}
private HtmlDocument GetDoc(string OuterHtml)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(OuterHtml);
return doc;
}
}
public class linkhref
{
public bool IsFirst = false;
public string Name;
public string Href;
}
public class part
{
public int PartIndex;
public string TagName;
public string Html;
public string ResultHtml;
public List<DataItem> items;
}
public class SingleDataItem
{
public int ID;
public string Name;
public string E001;
public string E002;
public string imagePath;
public string URL;
public string CreateTime;
}
public class DataItem
{
public int ID;
public string Name;
public string E001;
public string E002;
public string E003;
public string E004;
public string E005;
public string E006;
public string E007;
public string E008;
public string E009;
public string E010;
public string imagePath;
public string URL;
public string CreateTime;
}
}