HTMLAgility Pack无法找到PageMap标记

时间:2015-11-01 19:32:13

标签: c# html-agility-pack

我正在使用HTMLAgility包从HTML页面获取有关文章的信息。我能够在整个文档中找到我想要的东西,但由于某种原因,无论我做什么,我都找不到PageMap对象。我创建了一个测试文档,仅隔离了PageMap,但仍然没有运气。

这是测试HTML:

<html>
    <head>

        <PageMap>
            <DataObject type="document">
                <Attribute name="article_title">Test Title</Attribute>
                <Attribute name="article_publication_name">Test Publication Name</Attribute>
                <Attribute name="article_author">Test Authro | The Test</Attribute>
                <Attribute name="article_description">A test of test and test test test!</Attribute>
                <Attribute name="image_src">http://www.google.com</Attribute>
                <Attribute name="article_comments">0</Attribute>
                <Attribute name="article_date_original">10/31/2015</Attribute>
                <Attribute name="article_date_updated">10/31/2015</Attribute>
            </DataObject>
        </PageMap>


    </head>
    <body>
        test
    </body>
</html>

这是我正在使用的代码:

string strPageHTML = File.ReadAllText(@"test.htm");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(strPageHTML);

HtmlNode htmnArticle = doc.DocumentNode.SelectSingleNode("//PageMap");
tbMessagePreview.Text = htmnArticle.InnerHtml;

live或test HTML都加载正常,但htmnArticle节点始终为null。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

使用//pagemap(HtmlAgilityPack将节点规范化为小写 - HTML Agility Pack Parsing With Upper & Lower Case Tags?):

HtmlNode htmnArticle = doc.DocumentNode.SelectSingleNode("//pagemap");
tbMessagePreview.Text = htmnArticle.InnerHtml;

旁注:查看doc.DocumentNode.InnerHtml有助于了解节点的规范化方式。

相关问题