views:

167

answers:

3

Can someone tell me an efficient method of retrieving the CSS between tags on a page of markup in .NET?

I've come up with a method which uses recursion, Split() and CompareTo() but is really long-winded, and I feel sure that there must be a far shorter (and more clever) method of doing the same.

Please keep in mind that it is possible to have more than one element on a page, and that the element can be either or .

A: 

Here's a C# CSS parser. Should do what you need.

http://www.codeproject.com/KB/recipes/CSSParser.aspx

Sam
I already have the code to deal with the CSS, I just need a good, clean way of pulling the CSS into my class.The CSS parser you pointed to is more concerned with, like the title says, parsing the CSS for its attributes, which isn't what I'm looking to do here; I just want to GET the CSS.
awj
I've just downloaded the code and this looks very promising. I think the ideal solution is to use the regex from tcabls (above) and then parse the CSS with what you've suggested.
awj
+1  A: 

I'd probably go for HTML Agility Pack which gives you DOM style access to pages. It would be able to pick out your chunks of CSS data, but not actually parse this data into key/value pairs. You can get the relevant pieces of HTML using X-Path style expressions.

Edit: An example of typical usage of Html Agility Pack is shown below.

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
var nodes = doc.DocumentElement.SelectNodes("//a[@style"]);
//now you can iterate over the selected nodes etc
mdresser
This looks more like what I'm looking for, although I was really hoping for a simple, single method or two.I'll investigate and post back on my results. Thanks.
awj
This is quite simple - see my update for an example of typical usage.
mdresser
+1  A: 

Try Regex.

goto:http://gskinner.com/RegExr/ paste html with css, and use this expression at the top:

<style type=\"text/css\">(.*?)</style>

here is the c# version:

using System.Text.RegularExpressions;

Match m = Regex.Match(this.textBox1.Text, "<style type=\"text/css\">(.*?)</style>", RegexOptions.Singleline);

if (m.Success)
{
    string css = m.Groups[1].Value;
    //do stuff
}
tcables
Parsing html with Regex? Guys, you know what t
M28
What's wrong with Regex? You like using string.split() until you get your results?
tcables
This is actually the solution I went for. Very easy to implement and the most direct way of achieving what I want, though I will be taking a look at HTML Agility Pack in the near future.
awj