views:

88

answers:

3

I am trying to convert this code from csharp to vb. Tried to use third party tools, but not successful. Can some one help me .Thanks

private static string RemoveInvalidHtmlTags(this string text)
{
    return HtmlTagExpression.Replace(text, new MatchEvaluator((Match m) =>
    {
        if (!ValidHtmlTags.ContainsKey(m.Groups["tag"].Value))
            return String.Empty;

        string generatedTag = String.Empty;

        System.Text.RegularExpressions.Group tagStart = m.Groups["tag_start"];
        System.Text.RegularExpressions.Group tagEnd = m.Groups["tag_end"];
        System.Text.RegularExpressions.Group tag = m.Groups["tag"];
        System.Text.RegularExpressions.Group tagAttributes = m.Groups["attr"];

        generatedTag += (tagStart.Success ? tagStart.Value : "<");
        generatedTag += tag.Value;

        foreach (Capture attr in tagAttributes.Captures)
        {
            int indexOfEquals = attr.Value.IndexOf('=');

            // don't proceed any futurer if there is no equal sign or just an equal sign
            if (indexOfEquals < 1)
                continue;

            string attrName = attr.Value.Substring(0, indexOfEquals);

            // check to see if the attribute name is allowed and write attribute if it is
            if (ValidHtmlTags[tag.Value].Contains(attrName))
                generatedTag += " " + attr.Value;
        }

        // add nofollow to all hyperlinks
        //if (tagStart.Success && tagStart.Value == "<" && tag.Value.Equals("a", StringComparison.OrdinalIgnoreCase))
        //    generatedTag += " rel=\"nofollow\"";

        if (tag.Value.ToString() == "object")
        {
            generatedTag += (tagEnd.Success ? " height=\"374\" width=\"416\"" + tagEnd.Value : ">");
        }
        else
        {
            generatedTag += (tagEnd.Success ? tagEnd.Value : ">");
        }


        return generatedTag;
    }));
}
+2  A: 

Have you tried this free tool?

klausbyskov
Yes, i have used the same tool to convert. but is giving me an error.
vamsivanka
+6  A: 

The problem converting this code is that you have a lambda expression with a multi-line statement body:

(Match m) =>
{
    ...a lot of code
}

Since VB9 doesn't support this, you'll want to put the code in brackets into its own function instead:

Private Function GetValue(m As Match) As String
   ....a lot of code
End Function

Then your RemoveInvalidHtmlTags code will look like this:

Return HtmlTagExpression.Replace(text, new MatchEvaluator(AddressOf GetValue))

You can use free tools to translate the rest of the code.

Meta-Knight
I will try it and let you know. Thanks
vamsivanka
+1. That's what the error message quoted by the OP means: "VB does not support anonymous methods/lambda expressions with a statement body"
MarkJ
Meta-Knight, Thank you. Your solution worked.
vamsivanka
A: 

Looks like a dupe of this question.

GrayWizardx