0

任何人都可以帮助将带有敏捷包的 Html 解析为单个字符串吗?

我正在尝试解析类似于以下格式的 Html,

<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>

我尝试使用以下方法来获取“p”和“li”和“blockquote”。但是,方法 .Descendants 为“p”、“li”和“blockquote”创建了单独的集合,但我需要将单个元素按顺序放置并将它们存储在单个字符串中。

 IEnumerable<HtmlNode> h3Tags = document.DocumentNode.Descendants("p"); foreach (var h3tag in h3Tags) {}

例如,我想要我的字符串存储,“这是第一个集合:List1 List2 List3 这是第二个集合 List1 List2”。

谢谢!

4

1 回答 1

2

使用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;
于 2012-08-09T09:06:38.153 回答