My program need to parse css files into an in-memory object format. Any advice on how this should be done ?
-
26I don't get why this question was closed. It has got a lot of votes from people apparently wondering the same thing. Just because different people might have different opinions about the best solution, what a great place to list those opinions for others with the same question. – Jonathan Wood Sep 06 '13 at 15:31
-
3@JonathanWood - I entirely agree, but the policy is that tool recommendations are off topic, and it is asking for a tool recommendation. So closing it is appropriate, even if frustrating. – Bobson Oct 18 '13 at 13:22
-
I've rewroten the question so it can be reopened – Softlion Oct 20 '15 at 09:56
-
@Softlion, by doing so you seem to have invalidated all six of the existing answers, none of which talk about Linq. IMO it would be preferable to rollback, post a new question, and then link to that new question in a comment on this one. – Peter Taylor Oct 21 '15 at 07:33
-
You were right, I removed "Linq" as it was not present in the initial question. – Softlion Oct 21 '15 at 13:31
-
When I searched for a css parser in C#, I stumbled upon this question so it is obviously valuable. – Anders Lindén Dec 02 '20 at 11:37
6 Answers
ExCSS (supports CSS2.1 and CSS3) on GitHub: https://github.com/TylerBrinks/ExCSS.
Which is a newer version of the code project article: http://www.codeproject.com/KB/recipes/CSSParser.aspx

- 10,454
- 9
- 55
- 60

- 47,710
- 52
- 130
- 168
-
wish it would parse out the media query into an object model. it's not particularly hard to do so but all it gives you is a string – Simon_Weaver Dec 29 '14 at 02:57
-
1it works, but found a number of things it can't parse: `calc(50vw - 23em)` becomes `calc(50vw = 23em)` / `3rem` becomes `3` / `z-index: 2147483647` becomes `z-index: 2.147484E+09` / `*display: none` becomes `display: none` – Simon_Weaver Dec 30 '14 at 01:47
-
1It also does not handle `@-ms-viewport` returning `{System.Collections.Generic.List\`1[ExCSS.RuleSet]}` in output. – Herman Kan Apr 28 '15 at 07:01
And a slightly slower search turns up the blog post "CSS parser class in .NET" which embeds this gist on GitHub (in case the blog ever dies).

- 28,290
- 8
- 63
- 72
There is a CSS grammar file for GoldParser:
http://goldparser.org/grammars/files/css.zip
GoldParser is easy to include in a C# project, and generates an real LALR parser - not some regex hack.

- 4,684
- 3
- 31
- 55
-
LALR parser - not some regex Info on their site "GOLD grammars are based on Backus-Naur form and regular expressions" – Justin Jul 08 '09 at 21:57
-
1
-
This is a very simplified version of the CSS grammar. Instead I ported the CSS reference grammer (from the CSS 2.1 spec) to work with the GOLD parser ... and I've written a C# assembly to implement the rest of it (use the parser output to compute the style for an element in a DOM, using rule specificity, inheritance, default values, etc.). – ChrisW Sep 18 '09 at 14:24
-
1Goldparser is nice but it is extremely slow. Even in speed optimized C++ code it takes 10 seconds to parse a 15000 lines of code. If you compare this with the speed of the PHP parser this is extremely slow. – Elmue Sep 26 '13 at 02:54
Have you tried the one featured in JsonFx? It's written in C#, parses CSS3 syntax and is distributed under a MIT style license.

- 10,712
- 4
- 44
- 91
-
2Looks like the best of a bad bunch (recursion using `goto` statements - ouch) – satnhak Jun 16 '11 at 14:30
-
4@B: `goto` is a great way to *avoid* recursion, because it avoids re-entering a function. It's a great alternative to risking a stack overflow when you have an potentially high number of iterations. – Triynko Aug 30 '11 at 17:29
-
...because you risk a stack overflow when you have a potentially high number of iterations. :D – James World Mar 26 '12 at 22:51
-
1
I wrote one, using the grammar specified in the CSS 2.1 spec. I have also released it now: for details, see http://www.modeltext.com/css/

- 54,973
- 13
- 116
- 224
-
this uses regular expressions which just isn't going to cut it for complex css – Simon_Weaver Dec 29 '14 at 01:27