如何通过敏捷性将Html解析为C#中的字符串?
有人能帮我用Agility pack将Html解析成一个字符串吗 我正在尝试解析类似以下格式的Html如何通过敏捷性将Html解析为C#中的字符串?,c#,html,parsing,html-agility-pack,C#,Html,Parsing,Html Agility Pack,有人能帮我用Agility pack将Html解析成一个字符串吗 我正在尝试解析类似以下格式的Html <blockquote>\n <p>Here is the first collection:<\/p>\n <ol>\n <li>List1<\/li>\n <li>List2<\/li>\n
<blockquote>\n
<p>Here is the first collection:<\/p>\n
<ol>\n
<li>List1<\/li>\n
<li>List2<\/li>\n
<li>List3<\/li>\n
<\/ol>\n
<p>Here is the second collection:<\/p>\n
<ol>\n
<li>List1<\/li>\n
<li>List2<\/li>\n
<\/ol>\n
<\/blockquote>
\n
这是第一个集合:\n
\n
列表1\n
列表2\n
列表3\n
\n
这是第二个集合:\n
\n
列表1\n
列表2\n
\n
我尝试使用以下方法来获得“p”和“li”以及“blockquote”。
然而,method.subjects为“p”、“li”和“blockquote”创建单独的集合,但我需要将单独的元素按顺序放置,并将它们存储在单个字符串中
IEnumerable<HtmlNode> h3Tags = document.DocumentNode.Descendants("p"); foreach (var h3tag in h3Tags) {}
IEnumerable h3Tags=document.DocumentNode.substands(“p”);foreach(h3Tags中的var h3tag){}
例如,我想要我的字符串存储,
“这是第一个集合:List1 List2 List3这是第二个集合List1 List2”
谢谢大家! 使用blockquote节点的InnerText属性。应该按预期顺序返回字符串
做点像
var blockQuoteNode = document.DocumentNode.Descendants("blockquote").First(); // or do a document.DocumentNode.SelectSingleNode(//put the exact xpath value of the blockquote element here...)
var stringsYouNeed = blockQuoteNode.InnerText;
对不起,我把问题说得更清楚了。如果blockquote包含多个“p”和“li”怎么办?因为.First()只返回第一个节点。谢谢.First()将返回整个第一个blockquote节点对象,包括其所有子元素。这些子元素只是可能属于p和li类型的其他节点。请记住,.First()方法仅用于访问文档根节点中的子节点集合(在本例中为blockquote)中的第一个节点。您可以指定哪个节点或指定特定的xpath值来选择所需的blockquote节点。另外,如果您尝试一下,您会注意到InnerText属性返回节点及其子节点的所有内部文本属性值。