How to fix html tags(which is missing the <open> & <close> tags) with HTMLAgilityPack

你说的曾经没有我的故事 提交于 2019-12-01 03:34:41

The library isn't intelligent enough to create the opening p where you put it, but it's intelligent enough to create the missing h1. And in general, it creates valid HTML always, but not always the one you would expect.

So this code:

        HtmlDocument doc = new HtmlDocument();
        doc.Load(yourhtml);
        doc.Save(Console.Out);

will dump this:

<div><h1> hello Hi</h1></div> <div>hi <p></div>

Which is not what you want, but is valid HTML. You can also add a little trick like this:

        HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed;
        HtmlDocument doc = new HtmlDocument();
        doc.Load(yourhtml);
        doc.Save(Console.Out);

that will dump this:

<div><h1> hello Hi</h1></div> <div>hi <p></p></div>

When doing HtmlAgilityPack.HtmlDocument.LoadHTML(yourhtml) HTMLAgilityPack will automatically fix the tags for you, and then you can access those tags using: HtmlAgilityPack.HtmlDocument.DocumentNode.OuterHTML

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!