2012-06-08 35 views
1

我想選擇的範圍標記的內部文本轉換爲字符串。C# - 使用HTMLAgilityPack到節點轉換爲字符串

<span id="course-text">ELEC1700/ELEC1700 2012 S1: Computer Engineering 1</span> 

所以上面會變成:「ELEC1700/ELEC1700 2012 S1:計算機工程1」

我已經嘗試了夫婦使用的innerText和的getAttributes組合,我只是不能得到它的工作。

string name; 

WebClient client = new WebClient(); 

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); 

htmlDoc.Load(new System.IO.StringReader(client.DownloadString(urlTxtBx.Text))); 

name = htmlDoc.DocumentNode.SelectSingleNode("//span[@id='course-text']").InnerText; 
+0

您是否正在從文本框加載文本?如果你使用html - > htmlDocument.LoadHtml(ASCIIEncoding.ASCII.GetString(htmlString)),試試這個嗎? – lionheart

+0

你有沒有確認HTMLDOC肯定包含了對xml的(因爲它看起來像你從網上得到它......難道,當你只是把樣品直接HTML(除去變量爲勝利而它的工作)。 – Chris

回答

0

我錯過了urlTxtBx.Text上的client.DownloadString。

上面的代碼現在工作正常:)

+0

如果使用'htmlDoc.LoadHtml(client.DownloadString(urlTxtBx.Text));'它爲您節省了一個字符串讀取器;-) –