2

I have a HTML document with the following markup:

<LINK REL="STYLESHEET" HREF="c87d971ab8bc48ee87f9ced1b1d5c6e2.css" CHARSET="ISO-8859-1" TYPE="text/css">

I'd like a solution in C# that would change the CSS to (the CSS file name changes every time):

<LINK REL="STYLESHEET" HREF="other.css" CHARSET="ISO-8859-1" TYPE="text/css">

How do I do this in C#?

6
  • Is this the only change you need to make? Are all the documents in a similar format? Commented Dec 21, 2011 at 15:18
  • "Efficient" as in "easiest to code" I presume? Commented Dec 21, 2011 at 15:18
  • 1
    So this is a .aspx page with c# behind the scenes? Or, are these two separate files: .html and .cs? Commented Dec 21, 2011 at 15:18
  • Would a string search and replace do the trick? Commented Dec 21, 2011 at 15:19
  • What Framework are you using? ASP.NET MVC? ASP.NET? Commented Dec 21, 2011 at 15:19

4 Answers 4

1

You should check out the Html Agility Pack:

What is exactly the Html Agility Pack (HAP)?

This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT (you actually don't HAVE to understand XPATH nor XSLT to use it, don't worry...). It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).

(quoted from their codeplex page)

Sign up to request clarification or add additional context in comments.

1 Comment

and do you have a good example with HTML agility Pack to start with ?
1

If you have fixed set of html files use regular expressions. In general case use Html Agility Pack.

Here's simple regex you can start with:

<LINK [^>]+ HREF="(?<name>\w+).css"

Comments

1

Howzabout just a simple String.Replace(oldstring, newstring)? There are any number of functions in the string classes. What have you tried?

1 Comment

the oldstring change every time i don't think it is a good idea but thank your for your answer
1

How about this

  string original = @"<LINK REL=""STYLESHEET"" HREF=""c87d971ab8bc48ee87f9ced1b1d5c6e2.css"" CHARSET=""ISO-8859-1";      
      string[] data = original.Split(new string[]{"HREF","CHARSET"},StringSplitOptions.None);
      string final = string.Format("{0} HREF=\"other.css\" CHARSET{1}",data[0],data[2]);

1 Comment

Please stop suggesting edits that add no value to the post. See: meta.stackexchange.com/questions/116741/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.