using System.Linq;
using System.Text;
using HtmlAgilityPack;
namespace HtmlDemo
{
class Program
{
static void Main(string[] args)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + "no.htm";
var doc = new HtmlDocument();
Encoding encoder = Encoding.GetEncoding("utf-8");
doc.Load(filePath, encoder);
//rect 框架
//string Html=string.Empty;
//foreach()
//{
// Html+=string.Format("",1);
//}
//page
//title //HtmlNodeCollection titleNodes = doc.DocumentNode.SelectNodes("//title");
//description
//解析控件
//HtmlNodeCollection inputNodes = doc.DocumentNode.SelectNodes("//input[@type='text']");
//HtmlNodeCollection textareaNodes = doc.DocumentNode.SelectNodes("//textarea");
//HtmlNodeCollection selectNodes = doc.DocumentNode.SelectNodes("//select");
//解析tables
gettables(doc);
}
static List<TemplateTable> gettables(HtmlDocument doc)
{
List<TemplateTable> tttables = null;
TemplateTable tttable = null;
//查找节点
HtmlNodeCollection titleNodes = doc.DocumentNode.SelectNodes("//table");
if (titleNodes != null)
{
tttables = new List<TemplateTable>();
foreach (var item in titleNodes)
{
tttable = SplitTable(item);
if (tttable != null)
{
tttables.Add(tttable);
}
}
}
return tttables;
}
static TemplateTable SplitTable(HtmlNode table)
{
TemplateTable tb = null;
Columns citem = null;
//查找th节点
HtmlNodeCollection titleNodes = table.SelectNodes("//th");
if (titleNodes != null)
{
tb = new TemplateTable();
tb.Cols = new List<Columns>();
citem = new Columns();
foreach (var item in titleNodes)
{
citem = new Columns();
citem.ColumnName = ClearText(item.InnerText);
tb.TableName = tb.TableName + "-" + citem.ColumnName;
tb.Cols.Add(citem);
}
}
return tb;
}
static string ClearText(string text)
{
text = text.Replace("\r\n", string.Empty);
return text.Trim();
}
}
public class TemplateTable
{
public string TableName { get; set; }
public string TableDes { get; set; }
public List<Columns> Cols { get; set; }
}
public class Columns
{
public string ColumnName { get; set; }
public int ColumnDes { get; set; }
}
}