Xml with a Repeater control

i'm trying to fill a menu list in my web page using an XML doc. The XML doc has the structure:

=================
SamplePageSubMenu.xml
=================
<?xml version="1.0" encoding="utf-8" ?>
<SubMenu>      
      <SubMenuItem>
            About
      </SubMenuItem>
</SubMenu>

*******************

My Code behind looks like: (only Page_Load() method shown)

=================
SamplePage.aspx.vb (I import "System.Xml" And "System.Xml.XmlNodeList")
=================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.PageTitle = "SamplePage.aspx"

        If Not Page.IsPostBack Then
            Dim objXmlDoc As New XmlDocument()
            Dim objNodeList As XmlNodeList
            objXmlDoc.Load(MapPath("SubMenus\SamplePageSubMenu.xml"))
            objNodeList = objXmlDoc.SelectNodes("SubMenu/SubMenuItem")
            rptSubMenu.DataSource = objNodeList
            rptSubMenu.DataBind()

            lblMsg.Text = MapPath("SubMenus/SamplePageSubMenu.xml")
        End If
End Sub

********************

And finally, the portion of the aspx file that is filled is:

==================
SamplePage.aspx
==================
<asp:repeater id="rptSubMenu" runat="server">
         <ItemTemplate>
      <li><%# ((System.Xml.XmlNode)Container.DataItem).SelectSingleNode("SubMenuItem").InnerText  %></li>
         </ItemTemplate>
</asp:repeater>

********************

I found this bit of code doing some google search...and its not working at all. The real xml file has several SubMenuItem tags in it and i would like to read those in with the repeater. Right now, i receive:

Compiler Error Message: BC30691: 'XmlNode' is a type in 'Xml' and cannot be used as an expression.

And it highlights this line:

<li><%# ((System.Xml.XmlNode)Container.DataItem).SelectSingleNode("SubMenuItem").InnerText  %></li>

...How do i get a simple repeater to work?

Thanks in advance
LVL 3
mattfairwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PeterFearnCommented:
hi there,

change your <li> line to

<li><%# DataBinder.Eval(Container.DataItem, "InnerText")  %></li>
mb333Commented:
<?xml version="1.0" encoding="utf-8" ?>
<menus>
      <menu name="Products" url="#">
            <submenu name="product 1" url="http://www.domain.com/product1.aspx" />
            <submenu name="product 2" url="http://www.domain.com/product2.aspx" />
            <submenu name="product 3" url="http://www.domain.com/product3.aspx" />
      </menu>
      <menu name="Services" url="#">
            <submenu name="service 1" url="http://www.domain.com/service1.aspx" />
            <submenu name="service 2" url="http://www.domain.com/service2.aspx" />
      </menu>
      <menu name="Contact" url="http://www.domain.com/contact.aspx">
      </menu>
</menus>
mb333Commented:
Are you inrested to get an easy way to display the above xml file structure into a repeater?
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

PeterFearnCommented:
Your page language is vb.net, so this (c#) syntax will not work

((System.Xml.XmlNode)Container.DataItem). etc...

you could use CType(Container.DataItem, System.Xml.XmlNode) instead (not so sure on vb.net)

also, each data item is the actual node matched by /SubMenu/SubMenuItem, so using the SelectSingleNode method you are trying to access  /SubMenu/SubMenuItem/SubMenuItem, which doesn't exist, and so the InnerText will throw an error

and System.Xml.XmlNodeList is a type not a namespace, so I'm not sure how much mileage you'll get out of importing that ;)
mattfairwAuthor Commented:
how would something like that work, mb333?
mb333Commented:
First let's use the above xml file (menu.xml)

In your codebehind, add the following code into your page_load:

Dim DS As New DataSet
DS.ReadXml(Server.MapPath("menu.xml"))
RepeaterMenu.DataSource = DS.Tables(0).DefaultView
RepeaterMenu.DataBind()

Now to display our submenus we will use a repeater inside another repeater

<asp:repeater id="RepeaterMenu" runat="server" enableviewstate="false">
      <itemtemplate>
            <ul type="circle">
                  <li><a href='<%# Container.DataItem("url") %>'><%# Container.DataItem("name") %></a></li>
                        <asp:repeater id="SubMenu" runat="server" enableviewstate="false" datasource='<%# Container.DataItem.CreateChildView("menu_submenu")%>'>
                            <itemtemplate>
                                <ul type="disc">
                                    <li><a href='<%# Container.DataItem("url") %>'><%# Container.DataItem("name") %></a></li>
                                </ul>
                            </itemtemplate>
                        </asp:repeater>
            </ul>
      </itemtemplate>
</asp:repeater>


That's it!

mattfairwAuthor Commented:
how would i make it so i only returned the results for menus with the name Products from the xml file? or would it be easier to make a separate file for each menu to do something like that?
mb333Commented:
My code was more to display all the menus and submenus but try this using the same above xml file:

SamplePage.aspx.vb

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim DS As New DataSet
        DS.ReadXml(Server.MapPath("menu.xml"))
        RepeaterMenu.DataSource = DS.Tables(0).DefaultView
        RepeaterMenu.DataBind()

    End Sub

    Sub Item_Bound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If Not CType(e.Item.FindControl("lblMenu"), Label).Text = "Products" Then
                e.Item.Visible = False
            End If
        End If

    End Sub


SamplePage.aspx

<asp:repeater id="RepeaterMenu" runat="server" enableviewstate="false" OnItemDataBound="Item_Bound">
      <itemtemplate>
            <ul type="disc">
                  <asp:Label id="lblMenu" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "name") %>' visible="False"></asp:label>
            <asp:repeater id="SubMenu" runat="server" enableviewstate="false" datasource='<%# Container.DataItem.CreateChildView("menu_submenu")%>'>
                  <itemtemplate>
                        <li><a href='<%# Container.DataItem("url") %>'><%# Container.DataItem("name") %></a></li>
                  </itemtemplate>
            </asp:repeater>
                </ul>
      </itemtemplate>
</asp:repeater>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mattfairwAuthor Commented:
well that is wonderful :)  Thanks so much for all the time and effort!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.