Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

binding xml to a drop down list control

Posted on 2004-11-01
53
Medium Priority
?
827 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
Comment
Question by:SirReadAlot
  • 29
  • 11
  • 7
  • +1
53 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462061
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
ID: 12462094
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
ID: 12462099
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Rejojohny
ID: 12462101
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
ID: 12462103
SirReadAlot,
no it won't :) but the question what contains the liDataSource exist ?
B..M
0
 

Author Comment

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

Totally confused??

whats the write way
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462129
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
ID: 12462134
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
ID: 12462138
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
ID: 12462140
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
ID: 12462152
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
ID: 12462156
liDataSource has no values
0
 
LVL 26

Expert Comment

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

Author Comment

by:SirReadAlot
ID: 12462166
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
ID: 12462167
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
Rejojohny earned 400 total points
ID: 12462177
sorry again .. it has to be ..
Dim xmlEl As XmlElement, XmlList As XmlNodeList = doc.SelectNodes("TOPICLIST/TOPICS[@TYPE='Employees']")
0
 

Author Comment

by:SirReadAlot
ID: 12462184
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
ID: 12462198
rejo,
tried it to, no luck
0
 
LVL 22

Expert Comment

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

CJ.
0
 

Author Comment

by:SirReadAlot
ID: 12462203
guys!! still there?

do i need to bind the repeater control?
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:SirReadAlot
ID: 12462227
yes i did.
0
 

Author Comment

by:SirReadAlot
ID: 12462229
i fink the select node is alright
0
 

Author Comment

by:SirReadAlot
ID: 12462252
changed it Rejo, no luck.

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462255
SirReadAlot,

try this

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

B..M
0
 

Author Comment

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

Author Comment

by:SirReadAlot
ID: 12462302
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
ID: 12462313
do i need to edit the repeater control
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462383
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
ID: 12462419
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
CJ_S earned 800 total points
ID: 12462458
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
ID: 12462481
hang on i will try it
0
 

Author Comment

by:SirReadAlot
ID: 12462499
xmlnode is not defined
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12462514
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
ID: 12462527

Imports System.Xml

I have that in the code behind
0
 

Author Comment

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

Accepted Solution

by:
mmarinov earned 800 total points
ID: 12462534
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
ID: 12462538
ok, but no values
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12462543
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
ID: 12462551
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
ID: 12462559
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
ID: 12462566
go it
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462567
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
ID: 12462571
There are values!! got it!!
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462574
SirReadAlot,

congratulations
B..M
0
 

Author Comment

by:SirReadAlot
ID: 12462593
Thank to you guys!!!

3 problems solved, a millon to go!!!

How do i split these points
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462602
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
ID: 12462627
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
ID: 12462753
Remove the line Response.Write(XmlList.COunt)

:-)
0
 

Author Comment

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


You guys are great


thanks
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12462887
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
ID: 12462943
Glad to help!

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

971 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