binding xml to a drop down list control

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
SirReadAlotAsked:
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.

mmarinovCommented:
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
SirReadAlotAuthor Commented:
sorry i made a mistake in the question.  Would it be the same if it was a repeater control??

thanks
0
SirReadAlotAuthor Commented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

RejojohnyCommented:
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
mmarinovCommented:
SirReadAlot,
no it won't :) but the question what contains the liDataSource exist ?
B..M
0
SirReadAlotAuthor Commented:
the should contain nodes from the menu xml file.

Totally confused??

whats the write way
0
mmarinovCommented:
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
RejojohnyCommented:
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
SirReadAlotAuthor Commented:
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
SirReadAlotAuthor Commented:
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
mmarinovCommented:
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
SirReadAlotAuthor Commented:
liDataSource has no values
0
RejojohnyCommented:
xml is case sensitive .. use Employees instead of EMPLLOYEES
0
SirReadAlotAuthor Commented:
B.M,
I have tried
 Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICS/TOPIC[@TYPE='Employees']") but no repeater conrtol visible
0
RejojohnyCommented:
Also it has to be
Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICLIST/TOPIC[@TYPE='Employees']")
0
RejojohnyCommented:
sorry again .. it has to be ..
Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICLIST/TOPICS[@TYPE='Employees']")
0
SirReadAlotAuthor Commented:
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
SirReadAlotAuthor Commented:
rejo,
tried it to, no luck
0
CJ_SCommented:
SirReadALot,
A put the sample in the other thread. You must have lots of points to spend :-)

CJ.
0
SirReadAlotAuthor Commented:
guys!! still there?

do i need to bind the repeater control?
0
SirReadAlotAuthor Commented:
as long as i get an answer!!
0
RejojohnyCommented:
>>tried it to, no luck .. so u changed the selectnodes method??? still no value?
0
SirReadAlotAuthor Commented:
yes i did.
0
SirReadAlotAuthor Commented:
i fink the select node is alright
0
SirReadAlotAuthor Commented:
changed it Rejo, no luck.

0
mmarinovCommented:
SirReadAlot,

try this

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

B..M
0
SirReadAlotAuthor Commented:
tried it B.M, the control is not filled
0
SirReadAlotAuthor Commented:
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
SirReadAlotAuthor Commented:
do i need to edit the repeater control
0
mmarinovCommented:
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
SirReadAlotAuthor Commented:
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
CJ_SCommented:
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
SirReadAlotAuthor Commented:
hang on i will try it
0
SirReadAlotAuthor Commented:
xmlnode is not defined
0
CJ_SCommented:
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
SirReadAlotAuthor Commented:

Imports System.Xml

I have that in the code behind
0
SirReadAlotAuthor Commented:
am actually using a user control
0
mmarinovCommented:
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

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
SirReadAlotAuthor Commented:
ok, but no values
0
CJ_SCommented:
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
CJ_SCommented:
The code worked here. I also put it online for you to see:

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

0
SirReadAlotAuthor Commented:
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
SirReadAlotAuthor Commented:
go it
0
mmarinovCommented:
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
SirReadAlotAuthor Commented:
There are values!! got it!!
0
mmarinovCommented:
SirReadAlot,

congratulations
B..M
0
SirReadAlotAuthor Commented:
Thank to you guys!!!

3 problems solved, a millon to go!!!

How do i split these points
0
mmarinovCommented:
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
SirReadAlotAuthor Commented:
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
CJ_SCommented:
Remove the line Response.Write(XmlList.COunt)

:-)
0
SirReadAlotAuthor Commented:
yep thanks. i shall close this down.


You guys are great


thanks
0
mmarinovCommented:
SirReadAlot,

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

B..M
0
CJ_SCommented:
Glad to help!

mmarinov, I think it's okay like this :-)
0
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.