如何从html字符串获取DOM树? [英] How to get a DOM tree from a html string?

查看:526
本文介绍了如何从html字符串获取DOM树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在字符串变量中包含网页的源代码(html代码).我只想获取这些html代码的DOM树.我使用了HtmlDocument类,它具有HtmlElement-s,但似乎不是树状结构.而且我不知道如何使用HtmlDocument.DomDocument(MSDN没有帮助).

我只需要使用.Net框架工作在c#中执行此操作:

//字符串SourceWeb =< html网页代码.>
1)从SourceWeb获取DOM树.
2)访问此DOM树的根.
3)访问Root(或其他节点)的子代.

我想手动遍历树,而不是通过Id或TagName或类似的方式获取元素.

谢谢.

Hi,

I have the source codes (html codes) of a web page in a string variable. I just want to get a DOM tree of those html codes. I used HtmlDocument class and it has HtmlElement-s but it seems it''s not tree structured. And I don''t know how to use HtmlDocument.DomDocument (MSDN didn''t help).

I just need to do this in c# using .Net frame work:

// string SourceWeb = <html codes of a web page.>
1) Getting a DOM tree from SourceWeb.
2) Access to the Root of this DOM tree.
3) Access to children of Root (or other nodes).

I want to traverse the tree manually, instead of getting elements by Id or TagName or something like that.

Thank you.

推荐答案

我认为 HTML敏捷包对您有用.

http://htmlagilitypack.codeplex.com/ [ ^ ]

祝你好运.
I think that the Html Agility Pack will be useful for you.

http://htmlagilitypack.codeplex.com/[^]

good luck.


这篇关于如何从html字符串获取DOM树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆