0

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.

3
  • 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. Commented Sep 20, 2012 at 5:53
  • Thanks but How can I find all h1,h2 and h3 from stringbuilder? Commented Sep 20, 2012 at 5:55
  • 2
    HtmlAgilityPack Commented Sep 20, 2012 at 5:56

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for replying Its working fine but How can I give unique id to every anchor tag.
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

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.