c#html解析器,C#html解析器,解析HTML的各种⼯具⽐较。usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.Web;
usingSystem.IO;
namespaceIntrospectionHtml
{
publicpartialclassForm1 : Form
{
publicclassTreeNodeHtmlElm : TreeNode
{
//属性节点
publicenumTypeNode
{
Html,
Form,
Link,
Image,html document是什么
Css
};
publicHtmlElement mHtmlElement;
privateTypeNode type;
publicTypeNode Type
{
get{returntype; }
set{ type = value; }
publicTreeNodeHtmlElm(stringelm,TypeNode t)
:
base()
{
type = t;
Text = elm;
mHtmlElement = null;
}
publicTreeNodeHtmlElm(HtmlElement elm,stringtxt,TypeNode t) : base()
{
type = t;
Text = txt;
mHtmlElement = elm;
}
publicTreeNodeHtmlElm(HtmlElement elm,TypeNode t) :base() {
type = t;
mHtmlElement = elm;
try
{
if(elm.OuterText ==null|| elm.OuterText =="")
{
Text = elm.OuterHtml;
}
else
{
if(elm.OuterText.Length > 100)
{
Text = elm.OuterText.Substring(0, 100);
}
else
{
Text = elm.OuterText;
}
catch(Exception e)
{
Text = "";
}
}
};
TreeNodeHtmlElm nodeCss = null;
publicForm1()
{
InitializeComponent();
webBrowser2.Navigate("about:blank");
splitContainer1.SplitterDistance = 50;
}
privatevoidFillTree(HtmlElement hElmFather, TreeNodeHtmlElm t,TreeNodeHtmlElm.TypeNode type) {
foreach(HtmlElement hElminhElmFather.Children)
{
TreeNodeHtmlElm node = newTreeNodeHtmlElm(hElm,type);
t.Nodes.Add((TreeNode)node);
if(hElm.Children.Count > 0)
{
FillTree(hElm, node,type);
}
}
}
privatevoidFillTreeForm(HtmlDocument doc, TreeNodeHtmlElm t)
{
System.Collections.IEnumerator en = doc.Forms.GetEnumerator();
while(en.MoveNext())
{
FillTree((HtmlElement)en.Current,t,TreeNodeHtmlElm.TypeNode.Form);
List lstTemp = newList();
foreach(HtmlElement eindoc.Links)
{
stringtextToAdd = e.GetAttribute("href");
//
/
/ On elimine les doublons
//
if(lstTemp.IndexOf(textToAdd) == -1)
{
TreeNodeHtmlElm node = newTreeNodeHtmlElm(e, e.GetAttribute("href"), TreeNodeHtmlElm.TypeNode.Link); t.Nodes.Add((TreeNode)node);
lstTemp.Add(textToAdd);
}
}
}
privatevoidFillTreeImage(HtmlDocument doc, TreeNodeHtmlElm t)
{
List lstTemp = newList();
foreach(HtmlElement eindoc.Images)
{
stringtextToAdd = e.GetAttribute("src");
//
// On elimine les doublons
//
if(lstTemp.IndexOf(textToAdd) == -1 )
{
TreeNodeHtmlElm node = newTreeNodeHtmlElm(e, textToAdd,TreeNodeHtmlElm.TypeNode.Image);
t.Nodes.Add((TreeNode)node);
lstTemp.Add(textToAdd);
}
foreach(HtmlElement eindoc.All)
{
if(e.TagName.ToLower() =="link")
{
if(e.GetAttribute("rel").ToLower() =="stylesheet")
{
TreeNodeHtmlElm node = newTreeNodeHtmlElm(e, e.GetAttribute("href"),TreeNodeHtmlElm.TypeNode.Css); t.Nodes.Add((TreeNode)node);
}
}
}
}
privatevoidShowInHtmlPreview()
{
TreeNodeHtmlElm tn = (TreeNodeHtmlElm)(treeView1.SelectedNode);
if(tn !=null)
{
try
{
webBrowser2.Document.Body.InnerHtml = "
"+ tn.mHtmlElement.InnerHtml +"";
}
catch(Exception exp)
{
}
}
}
privatevoidSaveTreeNodeHtml(stringfilename)
{
// Mettre ceci dans un objet.
TreeNodeHtmlElm tn = (TreeNodeHtmlElm)(treeView1.SelectedNode);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论