Question : Parsing .aspx page with XMLReader, get a list of controls

Hi all,

I'm trying to get a list of control names from a .aspx page using:

Dim fileName As String = Server.MapPath("/lms/" & ddlPageList.SelectedItem.ToString)

        Dim objXmlReader As System.Xml.XmlTextReader = New System.Xml.XmlTextReader(fileName)
        While objXmlReader.Read
            If objXmlReader.Name = "ID" Then
                objXmlReader.MoveToAttribute("id")
                Dim id As String = objXmlReader.Value

                ddlControlList.Items.Add(id)
            End If
        End While

However I'm getting the following error:

System.Xml.XmlException was unhandled by user code
  Message="Name cannot begin with the '%' character, hexadecimal value 0x25. Line 1, position 2."
  Source="System.Xml"
  StackTrace:
       at System.Xml.XmlTextReaderImpl.Throw(Exception e)
       at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
       at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
       at System.Xml.XmlTextReaderImpl.ParseQName(Boolean isQName, Int32 startOffset, Int32& colonPos)
       at System.Xml.XmlTextReaderImpl.ParseElement()
       at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at System.Xml.XmlTextReader.Read()
       at administrator_languageLiterals.ddlPageList_SelectedIndexChanged(Object sender, EventArgs e) in C:\Inetpub\wwwroot\lms\administrator\languageLiterals.aspx.vb:line 104
       at System.Web.UI.WebControls.ListControl.OnSelectedIndexChanged(EventArgs e)
       at System.Web.UI.WebControls.DropDownList.RaisePostDataChangedEvent()
       at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent()
       at System.Web.UI.Page.RaiseChangedEvents()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Which I assume is falling down on the page declaration of the ASPX page:

<%@ Page Language="VB" MasterPageFile="~/jcb_1col.master" AutoEventWireup="false" EnableViewState="true"

It doesn't seem to like the <% which I assume is because it is expecting a XML file, however this method was suggested to me in another question, so I'm open to better ways of doing it...

TIA.

Answer : Parsing .aspx page with XMLReader, get a list of controls

Here's what I got it to work with:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Dim sr As StreamReader = New StreamReader(Server.MapPath("/lms/" & ddlPageList.SelectedItem.ToString))
        Dim line As String = sr.ReadToEnd
        Dim matches As MatchCollection
 
        Dim regex As New Regex("|>)", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
        matches = regex.Matches(line)
        sr.Close()
 
        Console.WriteLine("{0} matches in ", matches.Count)
 
        For Each match As Match In matches
            Dim groups As GroupCollection = match.Groups
            Dim test As String = groups.Item(0).Value
            ddlControlList.Items.Add(groups.Item(0).Value)
        Next
Random Solutions  
 
programming4us programming4us