0

I have a web app that allows users to edit a table. I have overwritten the Render method so that this table (with the edits) is saved as a .html file which I later convert to an image. There are a few things I don't want in the image -- specifically two buttons and a DropDownList. Here is the rendered HTML of interest:

<div class="centered" id="selectReport">
    <select name="Archives" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;Archives\&#39;,\&#39;\&#39;)&#39;, 0)" id="Archives" style="width:200px;">
        <option selected="selected" value="Dashboard_Jun-2012">Dashboard_Jun-2012</option>
    </select>
</div>

And here is the Render method:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {

        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();

        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        Regex regex = new Regex(@"<select name=""Archives""[.\\n]*(?<date>\\w{3}-\\d{4})[.\\n]*</select>");
        Match match = regex.Match(output);
        Response.Write(match.Success);
        string date = match.Groups["date"].Value;
        Regex.Replace(output, @"<select name=""Archives""[.\\n]*</select>", date);

        writer.Write(renderedContent);
    }
}

The two calls to Replace are working as expected and removing the two buttons. What I am attempting to do with the DropDownList is to replace it with the value it displays, however the pattern match is failing as Response.Write(match.Success) prints "False."

From my understanding this Regex should be working. Of course I have tried many alternatives but without any success.

Any advice is appreciated.

Regards.

Kevin
  • 1,252
  • 6
  • 32
  • 50
  • 2
    Have you read this: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ? – nhahtdh Aug 14 '12 at 21:49
  • 1
    Regex is [not a suitable tool for parsing HTML.](http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html) Consider using jQuery with some carefully-placed selectors to do this. – Robert Harvey Aug 14 '12 at 21:50
  • @nhahtdh: Thanks for the link. It was very informative. – Kevin Aug 15 '12 at 15:15

0 Answers0