Click here to Skip to main content
15,441,149 members
Articles / Web Development / ASP.NET
Posted 28 Apr 2004


51 bookmarked

ASP Parser

Rate me:
Please Sign up or sign in to vote.
3.90/5 (31 votes)
28 Apr 2004CPOL3 min read
This article presents a simple way to parse and analyze ASP document structure.


During my labor with ASP+, I developed a few design-time tools. I early encountered a problem that there was no way to analyze ASPX or ASCX document structure. Microsoft does not support something like ASP Document Object Model (similar to XML DOM). Therefore, I was forced to create my own ASP parser which analyzes the ASPX / ASCX document tags and creates a tree of objects that represent the structure of the document. The ASP DOM was very useful in my automatic localization mechanism but it could be used in many other cases.


When creating a new ASPX document, it usually looks similar to this one:

<%@ Page language="c#" Codebehind="Empty.aspx.cs" 
   AutoEventWireup="false" Inherits="PersistingObjects.Empty" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema 
  <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">

After parsing the document with the presented ASP DOM, it can be displayed as a tree of objects, e.g.:

Image 1

You can find here the parsed tree of tags that occur in the ASPX document. Each of the tags (including <OPEN_TAG>, </CLOSE_TAG>, <EMPTY_TAG/>, and tags that do not require closing tag, e.g.: <META>, <BR>) is represented by a tree node with a collection of attributes and child tags beneath it.


To create the ASP Document, use the following code:

string asp = 
  "<asp:linkbutton id=\"LinkButton1\" runat="\""server\">text</asp:linkbutton>";
ASP.Document root = new ASP.Document(asp);

After the ASP.Document is created, it can be traversed recursively, e.g.:

public void Traverse(ASP.Tag tag)
    foreach(ASP.Tag child in tag.ChildTags)

        foreach(ASP.Attribute attribute in child.Attributes)
            Console.WriteLine(attribute.Key + "=" + attribute.Value);


As the ASP.Document is inherited from ASP.Tag, the Traverse method can be called like this:



The core object of the parsed ASPX/ASCX is ASP.Tag. There are a few types of tags: Root, Open, Close, Text, Directive, Codeand Comment. The type of the ASP.Tag can be checked with Tag.TagType property.

  1. Root tag - the only tag that has the type of Root is ASP.Document. This tag type does not contain attributes.
  2. Open tag - e.g.: <title>. This tag type can contain attributes.
  3. Close tag - e.g.: </title>. This tag type does not contain attributes.
  4. Text tag - each text that occurs between other tags, e.g.: (<title>TEXT</title>, the TEXT will occur as a Text tag in ASP.Document tree). This tag type does not contain attributes.
  5. Directive tag - each tag that begins with %@, e.g.: <%@ Page language= "c#" ... %>. This tag type can contain attributes.
  6. Codetag - each tag that begins and ends with % (except the Directive which begins with %@, and Comment which begins with %--), e.g.: <% this.DoSomething() %>. This tag type does not contain attributes.
  7. Comment tag - XML comment, e.g.: <!--<span class="code-comment"> COMMENTED --></span>, or server side comment, e.g.: <%-- COMMENTED --%>. This tag type does not contain attributes.


Each of the ASP.Tag objects has Tag.Attributes property that contains a list of attributes of the tag. The list is empty if the tag does not contain attributes or its type does not support attributes. Therefore only Open or Directivetag can contain non empty attributes list.

Each of attributes contains key (Attribute.Key) and value (Attribute.Value). The value can be empty string ("") if the attribute does not contain it.


The main limitation I found in the presented ASP DOM is that it gives read only access to the parsed ASPX / ASCX document. It does not allow to modify the ASP.Document structure and save it back to a string.


This article does not describe all features of the presented ASP DOM but only briefly shows how to use it. I am sure that in most development cases, the ASP DOM is not needed, but I wanted to share what I gained to make your life easier. For those who will use it: enjoy it.

Source code attached to this article contains sample application that displays parsed ASPX document as a tree.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Web Developer
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralMy vote of 5 Pin
Manjeet Singh0211-Oct-11 0:00
MemberManjeet Singh0211-Oct-11 0:00 
GeneralVery nice, but... Pin
Member 77182519-Feb-09 11:40
MemberMember 77182519-Feb-09 11:40 
GeneralExcellent! Pin
artemgolubev28-Jan-09 14:15
Memberartemgolubev28-Jan-09 14:15 
GeneralAmazing, Pin
lagartron123mailcl21-Aug-08 13:27
Memberlagartron123mailcl21-Aug-08 13:27 
QuestionHow can i do it? Pin
Member 178404929-Jul-08 10:50
MemberMember 178404929-Jul-08 10:50 
AnswerRe: How can i do it? Pin
Marcin Celej30-Jul-08 6:56
MemberMarcin Celej30-Jul-08 6:56 
GeneralRe: How can i do it? Pin
Member 178404930-Jul-08 8:21
MemberMember 178404930-Jul-08 8:21 
GeneralRe: How can i do it? Pin
Marcin Celej30-Jul-08 11:21
MemberMarcin Celej30-Jul-08 11:21 
GeneralGreat job! Pin
hkenuam28-Jul-08 12:48
Memberhkenuam28-Jul-08 12:48 
GeneralRe: Great job! Pin
Marcin Celej30-Jul-08 6:57
MemberMarcin Celej30-Jul-08 6:57 
GeneralVery nice indeed! [modified] Pin
Alexandru Matei21-Jun-08 2:09
MemberAlexandru Matei21-Jun-08 2:09 
GeneralRe: Very nice indeed! Pin
Marcin Celej23-Jun-08 6:12
MemberMarcin Celej23-Jun-08 6:12 
GeneralRe: Very nice indeed! Pin
Alexandru Matei23-Jun-08 6:17
MemberAlexandru Matei23-Jun-08 6:17 
GeneralRe: Very nice indeed! Pin
Marcin Celej23-Jun-08 7:26
MemberMarcin Celej23-Jun-08 7:26 
QuestionHow can i use it? Pin
Wave004-Jun-08 3:54
MemberWave004-Jun-08 3:54 
AnswerRe: How can i use it? Pin
Marcin Celej4-Jun-08 10:25
MemberMarcin Celej4-Jun-08 10:25 
GeneralNice Work Pin
habanero7-Jun-06 2:03
Memberhabanero7-Jun-06 2:03 
QuestionInteresting! Pin
robert204828-Mar-06 3:00
Memberrobert204828-Mar-06 3:00 
AnswerRe: Interesting! Pin
Marcin Celej28-Mar-06 5:54
MemberMarcin Celej28-Mar-06 5:54 
GeneralRe: Interesting! Pin
robert204829-Mar-06 0:03
Memberrobert204829-Mar-06 0:03 
GeneralCool ! Pin
Trance Junkie24-Jun-05 0:10
MemberTrance Junkie24-Jun-05 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.