Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

DOM

A C# class library for parsing HTML into a searchable, filterable DOM tree

Basic usage

var html = "<html><head></head><body><div>Hello World!</div></body></html>";
var dom = Html.Parse(html, new ParserOptions()
{
ReplaceNbsp = " ",
TrimText = TrimType.OneTrailingSpace
});

Use LINQ queries

var elemText = dom.Elements.Where(el => el.Text.Contains("Hello")).FirstOrDefault();

Navigate DOM Hierarchy

// select all child nodes from all DIV tags
var nodes = dom.Elements.Where(el => el.TagName == "div").SelectMany((el, results) => el.Children());

Replace one Element with another


foreach(var elem in dom.Elements.Where(el => el.ClassNames.Contains("button")))
{
var button = elem.AllChildren.First(el => el.TagName == "a");
elem.ReplaceWith(button)
}

In the example above, we are replacing all elements that contain the ClassName "button" with an <a> tag found within each button childNode hierarchy using the AllChildren method.

ParserOptions

PropertyDefaultDescription
ReplaceNbsp&nbsp;Replaces HTML encoded spaces with the provided string
TrimTextTrimType.NoneTrims spaces from #text nodes. NOTE: Parser automatically removes any duplicate spaces from #text nodes.

TrimType

An enum used as ParserOptions.TrimText

LabelValueDescription
None0Parser does not trim any spaces from the beginning or end of all #text nodes
Right1Parser trims all spaces from the end of all #text nodes
Left2Parser trims all spaces from the beginning of all #text nodes
Both3Parser trims all spaces from the beginning and end of all #text nodes
OneTrailingSpace4Parser trims all spaces from the beginning and end of all #text nodes, and if there is a space at the end of the #text node, it will not be removed

About

A C# class library for parsing HTML into a searchable, filterable DOM tree

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages