项目作者: MSDN-WhiteKnight

项目描述 :
Helper library to assist in HTML creation with WinForms HtmlDocument class
高级语言: C#
项目地址: git://github.com/MSDN-WhiteKnight/HtmlDocumentFactory.git
创建时间: 2018-05-19T16:26:32Z
项目社区:https://github.com/MSDN-WhiteKnight/HtmlDocumentFactory

开源协议:BSD 3-Clause "New" or "Revised" License

下载


HtmlDocumentFactory

Windows Forms framework has a nice HtmlDocument class for HTML manupulation. However, it can only work with documents from WebBrowser control, and there’s no public constructor to create new HtmlDocument instance from scratch. This library attempts to fix this problem. It consists of a single static class HtmlLib.HtmlDocumentFactory that provides helper methods for creating and destroying HTML documents and converting them to strings.

Download binaries

Usage

Add reference to HtmlDocumentFactory.dll and import namespace with using HtmlLib; clause.

Create a new instance of HtmlDocument:

  1. HtmlDocument htmldoc = HtmlDocumentFactory.CreateHtmlDocument();

Modify document’s content like you wish:

  1. HtmlElement el = htmldoc.CreateElement("div");
  2. el.InnerText = "Hello, world!";
  3. el.Style = "color: red";
  4. htmldoc.Body.AppendChild(el);

Copy resulting HTML to string object:

  1. textBox1.Text = HtmlDocumentFactory.HtmlDocumentToString(htmldoc);

Free unmanaged resources when you no longer need the document:

  1. HtmlDocumentFactory.ReleaseHtmlDocument(htmldoc);

If you need to create HtmlDocument based on existing HTML content, pass a string into CreateHtmlDocument method:

  1. string html = "<p>Hello, world!</p>";
  2. HtmlDocument htmldoc = HtmlDocumentFactory.CreateHtmlDocument(html);