Skip to content Skip to sidebar Skip to footer

How Can I Find And Remove Css References In Html Head?

I have created a service to join, minify and compress css-references on a CMS system. Example: Before :

Solution 1:

Use HTML Agility Pack. Rough plan of attack:

  1. Load the html content into an HtmlDocument object.

  2. Find the link nodes in the HtmlDocument object via XPath

    var nodes = doc.DocumentBody.SelectNodes("//head/link[@type='text/css']");

  3. Retrieve the hrefs from those nodes

    string href = nodes[0].Attributes["href"].Value;

  4. Then replace the nodes with the new node.

Solution 2:

You can find the links that match your rules with regex:

<link href="(/Files/[^"]+)" .* media

It will give you the file path inside the quotes, e.g. '/Files/css1.css'. You can use that result to build up the string you wanted.

C# friendly regex:

@"<link href=""(/Files/[^""]+)"" .* media"

Use the Regex.Match method to get the groupings: http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx

Post a Comment for "How Can I Find And Remove Css References In Html Head?"