这是示例html页面,

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html version="-//W3C//DTD XHTML 1.1//EN" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test Page</title>
</head>
<body>
<div id="topContainer">
    <div id="header">
        <span>This is a Test message</span>
        <span id="slogan">A sample slogan <br /> with 2 lines.</span>
    </div>
    <div id="news">
        This is a test news
    </div>
</div>
</body>
</html>


这是我的C#代码,

    public MainPage()
    {
        InitializeComponent();
        HtmlWeb.LoadAsync("URL", DownLoadCompleted);

    }

    void DownLoadCompleted(object sender, HtmlDocumentLoadCompleted e)
    {
        if(e.Error == null)
        {
            HtmlDocument doc = e.Document;

            if (doc != null)
            {
                var newsdiv = (from divnode in doc.DocumentNode.Descendants("div")
                               where divnode.Attributes["id"].Value == "header"
                               select divnode).FirstOrDefault();

                var txtT = HttpUtility.HtmlDecode(newsdiv.InnerText);
                txtDisplay.Text = txtT;

            }
        }
    }


当我尝试检索header div的innerText时,它可以工作。但是,当我尝试相同的代码来检索topContainer div的内部文本时,它不会返回任何内容。它也不会引发错误。对于<span>元素,它根本不起作用。

可能是什么原因?

谢谢

最佳答案

似乎您正在尝试仅选择ID为IDOFTHEDIV的div;因此,不会选择具有其他ID(或没有ID)的其他DIV

10-07 19:53
查看更多