HtmlAgilityPack:无法解析结束行(忽略结束行)

HtmlAgilityPack : Can't parse endline (ignores end line)

我在解析以下内容时遇到问题 html:

<tr>
<td><p><b>
<span>Company:</span></b>
<span>Test</span>
</p></td>
</tr>

<tr>
<td><p><b>
<span>Company:</span></b>
<span>Test 2</span>
</p></td>
</tr>

我的代码是:

HtmlDocument doc = new HtmlDocument();
doc.Load(@"email.txt");
Console.WriteLine(doc1.DocumentNode.InnerText);

我有以下输出:Company:TestCompany:Test 2,但我想要

Company: Test
Company: Test 2

所以,问题是换行符没有被解析。

P.S.: doc.OptionWriteEmptyNodes = true; 没有区别.

更新: 我的意思是,无论 html 在那里,它都不会解析结束行。即使有<br />个标签等

您的 html 中没有 line-break。即使在您的浏览器中您也看不到它,这两个标签会并排显示。你的实际需求是什么? Document.InnerText 只是 returns 所有 text-controls 值并排显示。

如果你不想要你必须 select 你想要什么(f.e。所有跨度)然后使用 String.Join(Environment.NewLine, allInnerText)

var allInnerTexts = doc.DocumentNode.SelectNodes("//text()")
   .Select(n => n.InnerText.Trim())
   .Where(text => !String.IsNullOrEmpty(text));
Console.WriteLine(String.Join(Environment.NewLine, allInnerTexts));