tags:

views:

164

answers:

4

Is there a class in .NET for reading CSS files? I guess it would be the equivalent of what the XmlDocument class is to an XML file.

+1  A: 

Hi there.

I don't think there is a .NET built specifically for reading CSS files, but I did find an interesting forum post which might help.

Most likely the best thing to do would be to write your own CSS file reader.

Cheers. Jas.

Jason Evans
+2  A: 

There isn't anything built into the .NET framework's BCL (Base Class Library), so you would need to either roll your own CSS Parser, or use some other 3rd-party developed parser.

One such parser can be found here:

Simple CSS Parser

CraigTP
+1  A: 

It seems as if there isn't, but I managed to do what I needed by reading the entire file and doing a number or splits as follows:

} (right brace) to break into individual styles

{ (left brace) to break styles into element names and style values

, (comma) to split element names up

; (semicolon) to split styles up

: (colon) to split style names and values

I also needed to trim spaces, CRs and LFs at each stage and drop empty entries to make it neat.

I put the results into a ListDictionary where each entry contained a StringDictionary with the styles in.

That then allowed me to do the following to get all the styles element:

// C#
CssDocument css = new CssDocument();
css.Load("c:\mycssfile.css");
ListDictionary myBodyStyles = css["body"];  // to get all the syles for an element
string myBodyFontSize = css["body"]["font-size"];  // to get an individual style
Moose Factory
that's missing the "cascading" part of CSS though...
Gordon Carpenter-Thompson
@Gordon - I implemented the "cascading" calculations in my CSS parser, at http://www.modeltext.com/css/
ChrisW
A: 

Is there a class in .NET for reading CSS files?

I implemented one: for details see http://www.modeltext.com/css/

ChrisW