2017-02-20 20 views
0

我有這種形式的一些HTML:上Xamarin後裔HTML敏捷性包不工作

"\r 
<p>One<\/p>\r 
<p>Two<\/p>" 
"\r 
<p>Three<\/p>\r 
<p>Four<\/p>" 

它有這樣的,因爲我消費web服務,所以我不能改變的內容。我的問題是,當我嘗試獲取節點的innerText,然後將它傳遞給TextView時,它什麼也不做。我的代碼有問題嗎? Xamarin不允許使用SelectSingleNode作爲HTML敏捷性軟件包。這是我到目前爲止有:

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

foreach (HtmlNode node in doc.DocumentNode.Descendants("p")) 
     { 
      string currentText = node.InnerText; //Fetching the InnerText of the node 
      Console.WriteLine(currentText); // Testing if prints (It doesn't) 
     } 

回答

1

你應該更換<\/p></p>修復反應,使HTMLAgility可以解析它。

代碼:

var HtmlBlock = @"\r 
        <p>One<\/p>\r 
        <p>Two<\/p>"; 
var doc = new HtmlAgilityPack.HtmlDocument(); 
doc.OptionFixNestedTags = true; 
doc.LoadHtml(HtmlBlock.Replace(@"<\/", "</")); 

foreach (HtmlNode node in doc.DocumentNode.Descendants("p")) 
{ 
    string currentText = node.InnerText; //Fetching the InnerText of the node 
    Console.WriteLine(currentText); // Testing if prints (It doesn't) 
}