HTML Xamarin Descendant 上的 Agility Pack 无法正常工作

HTML Agility Pack on Xamarin Descendant not working

我有一些 HTML 这种形式:

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

它是这样的,因为我正在使用网络服务,所以我无法更改内容。我的问题是,当我尝试获取节点的 innerText,然后将其传递给 TextView 时,它什么也不做。我的代码有问题吗?我不能使用 SelectSingleNode,因为 Xamarin 上的 HTML Agility Pack 不允许这样做。这是我目前拥有的:

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)
        }

您应该通过将 <\/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)
}