https://www.cnblogs.com/jjccx/articles/238476.html
https://www.cnblogs.com/luqingfei/archive/2007/08/03/842093.html
https://www.cnblogs.com/brady-wang/p/8946094.html
c# html格式化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HtmlAgilityPack;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "b.txt");
HtmlAgilityPack.HtmlDocument doc = GetDoc(html);
//category_01关联关系
HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//li");
List<Menu> ms = null;
if (nodes != null && nodes.Count > 0)
{
ms = new List<Menu>();
Menu m = null;
foreach (HtmlNode n in nodes)
{
m = new Menu();
m.Name = n.InnerText.Trim();
HtmlAgilityPack.HtmlDocument subdoc = GetDoc(n.OuterHtml);
HtmlNodeCollection subs = subdoc.DocumentNode.SelectNodes("//a");
if (subs != null && subs.Count > 0)
{
m.SubMenu = new List<Menu>();
Menu sm = null;
foreach (HtmlNode sn in subs)
{
sm = new Menu();
sm.Name = sn.InnerText.Trim();
m.SubMenu.Add(sm);
}
}
ms.Add(m);
}
}
//string html = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "a.txt");
//HtmlAgilityPack.HtmlDocument doc = GetDoc(html);
category_01关联关系
//HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//li[@class=\"cate_menu_item\"]");
//List<Menu> ms = null;
//if (nodes != null && nodes.Count > 0)
//{
// ms = new List<Menu>();
// Menu m = null;
// foreach (HtmlNode n in nodes)
// {
// m = new Menu();
// m.Name = n.InnerText.Trim();
// HtmlAgilityPack.HtmlDocument subdoc = GetDoc(n.OuterHtml);
// HtmlNodeCollection subs = subdoc.DocumentNode.SelectNodes("//a[@class=\"cate_menu_lk\"]");
// if (subs != null && subs.Count > 0)
// {
// m.SubMenu = new List<Menu>();
// Menu sm = null;
// foreach (HtmlNode sn in subs)
// {
// sm = new Menu();
// sm.Name = sn.InnerText.Trim();
// m.SubMenu.Add(sm);
// }
// }
// ms.Add(m);
// }
//}
if (ms != null && ms.Count > 0)
{
}
}
static HtmlAgilityPack.HtmlDocument GetDoc(string html)
{
HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
return doc;
}
}
}