I have read a html file as string builder.Now I want to put anchor tag between h1,h2,and h3 and to give different id and href link. So how can I achive this.I want following thing to do.
I have tried Sb.Replace("<h1>", "<h1> <a id=1>"); but I can not give uniqe Id to anchor tag.So how can I read all h1,h2 and h3 and put anchor tag and give unique id to anchor tag.
-
You can't do this in one hit. Might be better off using RegEx and then doing replace 1 at a time and incrementing your ids.lahsrah– lahsrah2012-09-20 05:53:05 +00:00Commented Sep 20, 2012 at 5:53
-
Thanks but How can I find all h1,h2 and h3 from stringbuilder?Hitesh– Hitesh2012-09-20 05:55:31 +00:00Commented Sep 20, 2012 at 5:55
-
2HtmlAgilityPackL.B– L.B2012-09-20 05:56:06 +00:00Commented Sep 20, 2012 at 5:56
Add a comment
|
1 Answer
You can call Regex.Replace in the System.Text.RegularExpressions namespace and define a custom MatchEvaluator callback where you assign the new ids.
Something like the following:
var regHeaders = new Regex(@"<(?<close>/)?h(?<header>\d)\s*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var replaced = regHeaders.Replace(sb.ToString(), new MatchEvaluator(EvaluateHeaders));
And define the EvaluateHeaders callback something like this:
private static string EvaluateHeaders(Match m)
{
bool closeTag = m.Groups["close"].Success;
switch (int.Parse(m.Groups["header"].Value))
{
case 1: // h1
return closeTag ? "</a></h1>" : "<h1><a href=\"header1\">Header1";
// todo: your own implementation of the various other headers.
default:
return m.Value;
}
}
EDIT
In light of your latest comment, I've changed the code to the following:
var regHeaders = new Regex(@"<h(?<header>\d)\s*>(?<content>.+?)</h\1>", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
var replaced = regHeaders.Replace(sb.ToString(), EvaluateHeaders);
private static string EvaluateHeaders(Match m)
{
switch(int.Parse(m.Groups["header"].Value))
{
case 1: // <h1>content</h1>
return string.Format("<h1><a href=\"#\" id=\"{0}\">{0}</a><h1>", m.Groups["content"].Value);
default:
return m.Value;
}
}
2 Comments
Hitesh
Thank you for replying Its working fine but How can I give unique id to every anchor tag.
Hitesh
Now I want anchor id something different then using int variable.I want id for anchor as below:For Example :<h1>Test</h1> so I want something like <h1><a anchor id ="Test" href="mylink">Test</a></h1>.In Short my anchor id will be my content between h1 ,h2 and h3 tag.Thanks