|
 |
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
|
|
© Copyright 2003 Jesse Ezell.
|
|
|