HTML敏捷性包 - 从HTML文档获取HTML片段 [英] Html Agility Pack - Get html fragment from an html document

查看:196
本文介绍了HTML敏捷性包 - 从HTML文档获取HTML片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用HTML敏捷包;我将如何提取完整的HTML文件的HTML片段?对于我而言,一个html片段被定义为<内部的所有内容;车身方式> 标签

Using the html agility pack; how would I extract an html "fragment" from a full html document? For my purposes, an html "fragment" is defined as all content inside of the <body> tags.

例如:

输入示例:

<html>
   <head>
     <title>blah</title>
   </head>
   <body>
    <p>My content</p>
   </body>
</html>



所需的输出:

<p>My content</p>



在理想情况下,我想返回的内容不变,如果不包含< HTML> <身体GT; 元素(例如:假设我是通过一个片段摆在首位,如果它WASN。 'T完整的HTML文件)

Ideally, I'd like to return the content unaltered if it didn't contain an <html> or <body> element (eg. assume that I was passed a fragment in the first place if it wasn't a full html document)

任何人都可以点我在正确的方向?

Can anyone point me in the right direction?

推荐答案

我认为你需要做这捆柴。

I think you need to do it in pieces.

您可以做身体或HTML文件的selectNodes如下:

you can do selectnodes of document for body or html as follows

doc.DocumentNode.SelectSingleNode("//body") // returns body with entire contents :)

,那么你可以检查的标准空值,如果该提供,您可以根据它是以字符串。

then you can check for null values for criteria and if that is provided, you can take the string as it is.

希望它能帮助:)

这篇关于HTML敏捷性包 - 从HTML文档获取HTML片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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