the_farmer
the_farmer

Reputation: 669

How to convert style text to C# object such as class/hashTable/collection?

I have style text like this one:

".abc {border: 1px solid blue;color:black;...} 
.abc{background-image:url('http://example.com/images/a.png')...}
#abcd {color: blue}..."

I need to edit this text in the server (change background-image or add color property... ) and then save it as text.

I think the best way is to convert this text to a c# object such class/hashTable/collection ...

can some one help me with this issue ?

Thanks.

Upvotes: 0

Views: 960

Answers (2)

Raja Nadar
Raja Nadar

Reputation: 9499

Use the CssStyleCollection in-built class.

System.Web.UI.WebControls.Style style = new System.Web.UI.WebControls.Style(); 
// you can set various properties on style object.

CssStyleCollection cssStyleCollection = style.GetStyleAttributes(SOME_USER_CONTROL OR YOUR PAGE);
cssStyleCollection.Add("border", "1px solid blue"); // etc

The other option is to use the following structure:

List<KeyValuePair<string, List<KeyValuePair<string, string>>> cssValues = new List<KeyValuePair<string, List<KeyValuePair<string, string>>>();

cssValues.Add(new KeyValuePair<string, List<KeyValuePair<string, string>>("abc", new List<KeyValuePair<string, string>>
{
 new KeyValuePair<string, string>("border", "1px solid blue"),
 new KeyValuePair<string, string>("color", "black"),
 // so on
}));

we use list of KeyValuePairs instead of dictionary, because CSS classes can repeat and there is no guarantee of uniqueness.

Upvotes: 0

Matthew Dresser
Matthew Dresser

Reputation: 11442

My advice would be to keep as little style information in your C# code as possible. It would be better to define different classes in your CSS files corresponding to different styles and then to deal only with class names server side.

Upvotes: 1

Related Questions