Solved

binding xml to a drop down list control

Posted on 2004-11-01
804 Views
Last Modified: 2008-01-09
Hi, how do i fill a dropdown control with my xml file, using the code below. so far the dropdown list is not pppulated.

thanks in advance

 If (Not Page.IsPostBack) Then
            Dim doc As New XmlDocument, i As Int32, liDataSource As New ListItemCollection

            Dim XmlDoc As XmlDocument = New XmlDocument
            XmlDoc.Load(Server.MapPath("Menu.xml"))

            Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='EMPLOYEES']")
            For Each xmlEl In XmlList
                liDataSource.Add(New ListItem(xmlEl.Value))
            Next
            DropDownList1.DataSource = liDataSource
            DropDownList1.DataBind()

        End If
0
Question by:SirReadAlot
    53 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi SirReadAlot,
    are you sure that liDataSource has elements?
    also you have to set which column will be the DataTextField and DataValueField

    like
                DropDownList1.DataSource = liDataSource
                DropDownList1.DataTextField = "column name"
                DropDownList1.DataValueField = "column name"
                DropDownList1.DataBind()

    Regards!
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    sorry i made a mistake in the question.  Would it be the same if it was a repeater control??

    thanks
    0
     

    Author Comment

    by:SirReadAlot
    If (Not Page.IsPostBack) Then
                Dim doc As New XmlDocument, i As Int32, liDataSource As New ListItemCollection

                Dim XmlDoc As XmlDocument = New XmlDocument
                XmlDoc.Load(Server.MapPath("Menu.xml"))

                Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='EMPLOYEES']")
                For Each xmlEl In XmlList
                    liDataSource.Add(New ListItem(xmlEl.Value))
                Next
                Repeater1.DataSource = liDataSource
               Repeater1.DataBind()

            End If
    0
     
    LVL 26

    Expert Comment

    by:Rejojohny
    y don't u try this .. load the xml to a dataset and then bind it the normal way .. this is faster and more efficient and less code .. no loops ..

    Dim ds as new DataSet
    ds.ReadXml(Server.MapPath("Menu.xml"));

    with liDataSource.
        .Datasource = ds
        .DataTextField = urfieldname
        .DataValueField = urvalueField
        .databind
    end with
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,
    no it won't :) but the question what contains the liDataSource exist ?
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    the should contain nodes from the menu xml file.

    Totally confused??

    whats the write way
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    it should and it contains is totaly different - can you debug program and see if the liDataSource has elements
    also can you post the xml file ?

    B..M
    0
     
    LVL 26

    Expert Comment

    by:Rejojohny
    what r u confused about? u have a dropdownlist which has to populated with values from an xml file .. right? u could right a loop to do it the way u mentioned or u could also bind it the way i have sugested ..
    could u post a sample of ur xml file ..
    0
     

    Author Comment

    by:SirReadAlot
    hi johny, i need to use this code as i am selecting from different nodes, to fill a repeater control.

    thanks

    If (Not Page.IsPostBack) Then
                Dim doc As New XmlDocument, i As Int32, liDataSource As New ListItemCollection

                Dim XmlDoc As XmlDocument = New XmlDocument
                XmlDoc.Load(Server.MapPath("Menu.xml"))

                Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='EMPLOYEES']")
                For Each xmlEl In XmlList
                    liDataSource.Add(New ListItem(xmlEl.Value))
                Next
                Repeater1.DataSource = liDataSource
               Repeater1.DataBind()

            End If
    0
     

    Author Comment

    by:SirReadAlot
    here is the xml file

    <?xml version="1.0"?>
    <TOPICLIST TYPE="MenuItems">
    <TOPICS TYPE="Employees" Roles="admin">
       <TOPIC>
        <TITLE>Add</TITLE>
        <URL>Admin.aspx</URL>
         </TOPIC>
      <TOPIC>
        <TITLE>Edit</TITLE>
        <URL>AminEdit.aspx</URL>
          </TOPIC>
      <TOPIC>
        <TITLE>Search</TITLE>
        <URL>AminEdit.aspx</URL>
             </TOPIC>
           </TOPICS>

    <TOPICS TYPE="Clients" Roles="Clients">

      <TOPIC>
        <TITLE>Clients</TITLE>
        <URL>#</URL>
      </TOPIC>
      <TOPIC>
        <TITLE>Matter</TITLE>
        <URL>#</URL>
      </TOPIC>
      <TOPIC>
        <TITLE>TaskCodes</TITLE>
        <URL>#</URL>
      </TOPIC>
    </TOPICS>

    <TOPICS TYPE="Receipts">
    <TOPIC>
        <TITLE>ReceiptTypes</TITLE>
        <URL>#</URL>
          </TOPIC>
          <TOPIC>
        <TITLE>Expenses</TITLE>
        <URL>#</URL>
          </TOPIC>


      <TOPICS TYPE="Suppliers">
        <TOPIC>
          <TITLE>Add</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Edit</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Exit</TITLE>
          <URL>#</URL>
        </TOPIC>
      </TOPICS>

      <TOPICS TYPE="User Access &amp; Security">
        <TOPIC>
          <TITLE>Groups</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Groups Approvers</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Group Type</TITLE>
          <URL>#</URL>
        </TOPIC>
          </TOPICS>
     <TOPICS TYPE="System Administration">
        <TOPIC>
          <TITLE>System Params</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Country</TITLE>
          <URL>#</URL>
        </TOPIC>
        <TOPIC>
          <TITLE>Currency</TITLE>
          <URL>#</URL>
        </TOPIC>
       
      </TOPICS>
     
       </TOPICS>

    </TOPICLIST>


    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    try with this line
     Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='Employees']")
    the XML is case sensitive
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    liDataSource has no values
    0
     
    LVL 26

    Expert Comment

    by:Rejojohny
    xml is case sensitive .. use Employees instead of EMPLLOYEES
    0
     

    Author Comment

    by:SirReadAlot
    B.M,
    I have tried
     Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='Employees']") but no repeater conrtol visible
    0
     
    LVL 26

    Expert Comment

    by:Rejojohny
    Also it has to be
    Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICLIST/TOPIC[@TYPE='Employees']")
    0
     
    LVL 26

    Assisted Solution

    by:Rejojohny
    sorry again .. it has to be ..
    Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICLIST/TOPICS[@TYPE='Employees']")
    0
     

    Author Comment

    by:SirReadAlot
    this is what i have in the page load
    If (Not Page.IsPostBack) Then
                Dim doc As New XmlDocument, i As Int32, liDataSource As New ListItemCollection
                Dim XmlDoc As XmlDocument = New XmlDocument
                XmlDoc.Load(Server.MapPath("Menu.xml"))
                Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='Employees']")
                For Each xmlEl In XmlList
                    liDataSource.Add(New ListItem(xmlEl.Value))
                Next
                Repeater1.DataSource = liDataSource
                Repeater1.DataBind()

            End If
    0
     

    Author Comment

    by:SirReadAlot
    rejo,
    tried it to, no luck
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    SirReadALot,
    A put the sample in the other thread. You must have lots of points to spend :-)

    CJ.
    0
     

    Author Comment

    by:SirReadAlot
    guys!! still there?

    do i need to bind the repeater control?
    0
     

    Author Comment

    by:SirReadAlot
    as long as i get an answer!!
    0
     
    LVL 26

    Expert Comment

    by:Rejojohny
    >>tried it to, no luck .. so u changed the selectnodes method??? still no value?
    0
     

    Author Comment

    by:SirReadAlot
    yes i did.
    0
     

    Author Comment

    by:SirReadAlot
    i fink the select node is alright
    0
     

    Author Comment

    by:SirReadAlot
    changed it Rejo, no luck.

    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    try this

    Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("//TOPICS/TOPIC[@TYPE='Employees']")

    B..M
    0
     

    Author Comment

    by:SirReadAlot
    tried it B.M, the control is not filled
    0
     

    Author Comment

    by:SirReadAlot
    the repeater control is still invisible am still using this

            If (Not Page.IsPostBack) Then
                Dim doc As New XmlDocument, i As Int32, liDataSource As New ListItemCollection
                Dim XmlDoc As XmlDocument = New XmlDocument
                XmlDoc.Load(Server.MapPath("Menu.xml"))
                           Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='Employees']")
                For Each xmlEl In XmlList
                    liDataSource.Add(New ListItem(xmlEl.Value))
                Next
                Repeater1.DataSource = liDataSource
                Repeater1.DataBind()



            End If
    0
     

    Author Comment

    by:SirReadAlot
    do i need to edit the repeater control
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    the problem is with this line
     Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("//TOPICS[@TYPE='Employees']/TOPIC")

    you tring to find something within an initialized object doc but not XmlDoc
    so use this
     Dim xmlEl As XmlElement, XmlList As XmlNodeList = XmlDoc.SelectNodes("//TOPICS[@TYPE='Employees']/TOPIC")
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    gosh, still no data.

    As with a datagrid, don't i need to bind the repeater control or tell it what colums i.e columns[0].tostring??
    0
     
    LVL 22

    Assisted Solution

    by:CJ_S
    With you XML file you can use the following .aspx page:

    <%@ Page Language="VB" %>
    <%@ Import Namespace="System.Xml" %>

    <script runat="server">
    Private Sub Page_Load(sender as Object, e as SYstem.EventArgs)
          If (Not Page.IsPostBack) Then
                Dim i As Int32, liDataSource As New ListItemCollection
                Dim XmlDoc As XmlDocument = New XmlDocument
                XmlDoc.Load(Server.MapPath("Menu.xml"))
                Dim xmlEl As XmlElement, XmlList As XmlNodeList = XmlDoc.SelectNodes("//TOPICLIST/TOPICS[@TYPE='Employees']/TOPIC")
                Response.Write(XmlList.COunt)
                'For Each xmlEl In XmlList
                '      liDataSource.Add(New ListItem(xmlEl.innerText))
                'Next
                rptMenu.DataSource = XmlList
                rptMenu.DataBind()
          End if
    End Sub
    </script>

    <form runat="server">
    <asp:repeater id="rptMenu" runat="server">
     <HeaderTemplate>
     <table>
     </HeaderTemplate>
     <ItemTemplate>
     <tr><td><%# CType(Container.DataItem, XmlNode).SelectSingleNode("TITLE").innerText %></td><td><%# CType(Container.DataItem, XmlNode).SelectSingleNode("URL").innerText %></td></tr>
     </ItemTemplate>
     <FooterTemplate>
     </table>
     </FooterTemplate>
    </asp:repeater>
    </form>


    0
     

    Author Comment

    by:SirReadAlot
    hang on i will try it
    0
     

    Author Comment

    by:SirReadAlot
    xmlnode is not defined
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    Did you copy the whole code?

    Are you using a codebehind or all in one .aspx files?

    If you ARE using codebehinds or compiled librarties you need to import the Xml namespace in the .aspx page. Using the line
    <%@ Import Namespace="System.Xml" %>
    0
     

    Author Comment

    by:SirReadAlot

    Imports System.Xml

    I have that in the code behind
    0
     

    Author Comment

    by:SirReadAlot
    am actually using a user control
    0
     
    LVL 28

    Accepted Solution

    by:
    SirReadAlot,
    because you use code behind you have to change the line like this
     <tr><td><%# CType(Container.DataItem, System.Xml.XmlNode).SelectSingleNode("TITLE").innerText %></td><td><%# CType(Container.DataItem, System.Xml.XmlNode).SelectSingleNode("URL").innerText %></td></tr>
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    ok, but no values
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    Yea, that was the change for. Like I also said, you need to add the @Import Namespace line to the top of the page, underneath the @Page  directive.
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    The code worked here. I also put it online for you to see:

    http://www.soft-trax.com/test.aspx

    0
     

    Author Comment

    by:SirReadAlot
    done this===
    Yea, that was the change for. Like I also said, you need to add the @Import Namespace line to the top of the page, underneath the @Page  directive.

    done this==
    SirReadAlot,
    because you use code behind you have to change the line like this
     <tr><td><%# CType(Container.DataItem, System.Xml.XmlNode).SelectSingleNode("TITLE").innerText %></td><td><%# CType(Container.DataItem, System.Xml.XmlNode).SelectSingleNode("URL").innerText %></td></tr>
    B..M

    but no values in the repeater ctrl
    0
     

    Author Comment

    by:SirReadAlot
    go it
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    it work for me too - just copied the code from CJ, and make the correction i've posted
    check if you have set the correct repeater id

    B..M
    0
     

    Author Comment

    by:SirReadAlot
    There are values!! got it!!
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    congratulations
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    Thank to you guys!!!

    3 problems solved, a millon to go!!!

    How do i split these points
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,
    it depends on you - but if you mean technicaly , there should be Split POints link down the page, above the textarea
    B..M
    0
     

    Author Comment

    by:SirReadAlot
    question for cj, how do i get rid of the 3, at the top of the page

    http://www.soft-trax.com/test.aspx
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    Remove the line Response.Write(XmlList.COunt)

    :-)
    0
     

    Author Comment

    by:SirReadAlot
    yep thanks. i shall close this down.


    You guys are great


    thanks
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    SirReadAlot,

    i'm sorry but the correct answer was to CJ_S not mine - mine was a correction

    B..M
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    Glad to help!

    mmarinov, I think it's okay like this :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles al…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now