这是我拥有的超级简单的代码:
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.OptionWriteEmptyNodes = true;
htmlDoc.Load("sourcefilepath");
htmlDoc.Save("destfilepath", Encoding.UTF8);
输入:
<?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 xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8"/>
<link rel="stylesheet" href="main.css" type="text/css"/>
</head>
<body>lots of text here, obviously not relevant to this problem</body>
</html>
输出:
<?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 xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<link rel="stylesheet" href="main.css" type="text/css" />
</head>
<body>lots of text here, obviously not relevant to this problem</body>
</html>
您可以看到在第一行有一个错误: /> 而不是 ?> 如果我将 OptionWriteEmptyNodes 设置为 true 值,则会发生这种情况。它已设置为 true,否则元/链接标签(以及文档正文中的其他一些标签)将不会被关闭。
有谁知道如何解决这个问题?