Updated: 2/2/2003; 9:06:47 PM.
Jesse Ezell's Radio Weblog
.NET and Other Interesting Stuff
        

Thursday, January 09, 2003

So, I now have ASPX parsing working without the ProcessRequest method. Took a little bit of work, but it is a better solution in the end. Luckily, the HtmlParser was already built (at least mostly...did take a few minor enhancements to process code blocks and end tags for tags such as "<closedTag />", but that was a relatively painless process).  Still some work to do on making it more robust (like proper whitespace handling in the register directive), but this does get the job done on my test page.

    Stream aspxReader = File.OpenRead(path);

    HtmlTextReader htmlReader = new HtmlTextReader(aspxReader);

    htmlReader.PreserveCase = true;

    StringBuilder contentBuilder = new StringBuilder();

    bool inForm = false;

    while(htmlReader.Read())

    {

    if(htmlReader.NodeType == HtmlNodeType.Code)

    {

    if(htmlReader.Value.StartsWith("@Register") || htmlReader.Value.StartsWith("@ Register") )

    {

    contentBuilder.Append("<%");

    contentBuilder.Append(htmlReader.Value);

    contentBuilder.Append("%>");

    }

    }

    else

    {

    if(htmlReader.NodeType == HtmlNodeType.EndElement && String.Compare(htmlReader.Name, "form", true) == 0)

    {

    inForm = false;

    }

    if(inForm)

    {

    if(htmlReader.NodeType == HtmlNodeType.Element)

    {

    contentBuilder.Append("<");

    contentBuilder.Append(htmlReader.Name);

    for(int i = 0; i < htmlReader.AttributeCount; i++)

    {

    if(htmlReader["runat"] == "server" && htmlReader[i].StartsWith("<%"))

    {

    }

    else

    {

    contentBuilder.AppendFormat(" {0}=\"{1}" ", htmlReader.AttributeName(i), htmlReader[i]);

    }

    }

    contentBuilder.Append(">");

    }

    if(htmlReader.NodeType == HtmlNodeType.EndElement)

    {

    contentBuilder.AppendFormat("</{0}>", htmlReader.Name);

    }

    }

    if(htmlReader.NodeType == HtmlNodeType.Element && String.Compare(htmlReader.Name, "form", true) == 0)

    {

    inForm = true;

    }

}

    }

    string controlText = contentBuilder.ToString();

    Page templatePage = new Page();

    Control control = templatePage.ParseControl(controlText);

    templatePage.Controls.Add(control);


1:17:07 PM    comment []

© Copyright 2003 Jesse Ezell.
 
January 2003
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Dec   Feb


Click here to visit the Radio UserLand website.

Subscribe to "Jesse Ezell's Radio Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.