2
這裏是樣本HTML頁面內返回文本,HtmlAgilityPack沒有一個div
<?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>
元素根本不起作用。
可能是什麼原因?
感謝
請考慮添加顯示問題的示例HTML元素。 –
對不起,沒有足夠的信息。我剛剛添加了一個HTML代碼片段。和更多信息。謝謝。 – user1004912