Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 837847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:12:31+00:00 2026-05-15T05:12:31+00:00

I need to parse through the aspx file (from disk, and not the one

  • 0

I need to parse through the aspx file (from disk, and not the one rendered on the browser) and make a list of all the server side asp.net controls present on the page, and then create an xml file from it. which would be the best way to do it? Also, are there any available libraries for this?

For eg, if my aspx file contains

<asp:label ID="lbl1" runat="server" Text="Hi"></asp:label>

my xml file would be

<controls>
<ID>lbl1</ID>
<runat>server</runat>
<Text>Hi</Text>
</controls>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:12:31+00:00Added an answer on May 15, 2026 at 5:12 am

    Xml parsers wouldn’t understand the ASP directives: <%@ <%= etc.

    You’ll probably best to use regular expressions to do this, likely in 3 stages.

    1. Match any tag elements from the entire page.
    2. For Each tag, match the tag and control type.
    3. For Each tag that matches (2), match any attributes.

    So, starting from the top, we can use the following regex:

    (?<tag><[^%/](?:.*?)>)
    

    This will match any tags that don’t have <% and < / and does so lazily (we don’t want greedy expressions, as we won’t read the content correctly). The following could be matched:

    <asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">
    <asp:Image runat="server" />
    <img src="/test.png" />
    

    For each of those captured tags, we want to then extract the tag and type:

    <(?<tag>[a-z][a-z1-9]*):(?<type>[a-z][a-z1-9]*)
    

    Creating named capture groups makes this easier, this will allow us to easily extract the tag and type. This will only match server tags, so standard html tags will be dropped at this point.

    <asp:Content ID="ph_PageContent" ContentPlaceHolderID="ph_MainContent" runat="server">
    

    Will yield:

    { tag = "asp", type = "Content" }
    

    With that same tag, we can then match any attributes:

    (?<name>\S+)=["']?(?<value>(?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?
    

    Which yields:

    { name = "ID", value = "ph_PageContent" },
    { name = "ContentPlaceHolderID", value = "ph_MainContent" },
    { name = "runat", value = "server" }
    

    So putting that all together, we can create a quick function that can create an XmlDocument for us:

    public XmlDocument CreateDocumentFromMarkup(string content)
    {
      if (string.IsNullOrEmpty(content))
        throw new ArgumentException("'content' must have a value.", "content");
    
      RegexOptions options = RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase;
      Regex tagExpr = new Regex("(?<tag><[^%/](?:.*?)>)", options);
      Regex serverTagExpr = new Regex("<(?<tag>[a-z][a-z1-9]*):(?<type>[a-z][a-z1-9]*)", options);
      Regex attributeExpr = new Regex("(?<name>\\S+)=[\"']?(?<value>(?:.(?![\"']?\\s+(?:\\S+)=|[>\"']))+.)[\"']?", options);
    
      XmlDocument document = new XmlDocument();
      XmlElement root = document.CreateElement("controls");
    
      Func<XmlDocument, string, string, XmlElement> creator = (document, name, value) => {
        XmlElement element = document.CreateElement(name);
        element.InnerText = value;
    
        return element;
      };
    
      foreach (Match tagMatch in tagExpr.Matches(content)) {
        Match serverTagMatch = serverTagExpr.Match(tagMatch.Value);
    
        if (serverTagMatch.Success) {
          XmlElement controlElement = document.CreateElement("control");
    
          controlElement.AppendChild(
            creator(document, "tag", serverTagMatch.Groups["tag"].Value));
          controlElement.AppendChild(
            creator(document, "type", serverTagMatch.Groups["type"].Value));
    
    
          XmlElement attributeElement = document.CreateElement("attributes");
    
          foreach (Match attributeMatch in attributeExpr.Matches(tagMatch.Value)) {
            if (attributeMatch.Success) {
              attributeElement.AppendChild(
                creator(document, attributeMatch.Groups["name"].Value, attributeMatch.Groups["value"].Value));
            }
          }
    
          controlElement.AppendChild(attributeElement);
          root.AppendChild(controlElement);
        }
      }  
    
      return document;
    }
    

    The resultant document could look like this:

    <controls>
      <control>
        <tag>asp</tag>
        <type>Content</type>
        <attributes>
          <ID>ph_PageContent</ID>
          <ContentPlaceHolderID>ph_MainContent</ContentPlaceHolderID>
          <runat>server</runat>
        </attributes>
      </control>
    </controls>
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 419k
  • Answers 419k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unfortunately, there isn't. As long as the iframe is on… May 15, 2026 at 10:20 am
  • Editorial Team
    Editorial Team added an answer I think part of the issue here is that there… May 15, 2026 at 10:20 am
  • Editorial Team
    Editorial Team added an answer ProgressDialog dialog; @Override protected void onPreExecute() { dialog = new… May 15, 2026 at 10:20 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.