Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate dropdownlist from multiple fields in xml file

Status
Not open for further replies.

may1hem

Programmer
Jul 28, 2002
262
GB
I have an xml file which lists events: the event name, the weekday, the start time, and the end time.

I want to populate a dropdownlist on my web page with these events. E.g. it should display events as:
Monday 7pm to 8pm dance group
Tuesday 6:30pm to 9pm circuits class

Obviously this requires joining multiple XML fields together when inserting into the dropdownlist.

How can I do this? Btw I'm coding in VB.net. Thanks for any advice.
 
1. load the xml into a reader/xmldocument.
Code:
var id = 1;
var xml = new XmlDocument("path to xml file");
foreach(var node in xml.SelectNodes("\\event")
{
    yield return new Event
       {
          Id = id++,
          Name = node["Name"];
          Start = DateTime.Parse(node["Start"]),
          End = DateTime.Parse(node["End"]),
       };
}
2. parse the document into an explicit object
Code:
class Event
{
   public int Id {get;set;}
   public string Name {get;set;}
   public DateTime Start {get;set;}
   public DateTime End {get;set;}

   public String DayOfWeek {get{return Start.DayOfWeek.ToString();}
   public string DisplayText {get{return string.Format("{0:DDDD h:mt} to {1:h:mt} {2}", Start, End, Name)}}
}
then you can use a collection of events as the datasource for your list.
Code:
MyDropDownList.DataSource = GetEvents();
MyDropDownList.DataTextField = "DisplayText";
MyDropDownList.DataValueField = "Id";
MyDropDownList.DataBind();

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks for your advice, though I'm coding in VB.net, I've never coded in C#.

How do I do this in VB.net?
 
even if you cannot read the code the steps are outlined. the code is just for convenience.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I'm fairly new to ASP.net programming and don't quite follow what's going on.

Is "GetEvents" an ASP.net keyword? I don't see it defined anywhere in the code.

Where do I create the "class Event" code? Does that have to go somewhere in particular in my code?

The fact that the code is written in C# doesn't help me. I tried to use the online converter but for the first piece of code it gave me this error: "Statement fragment: please enter a complete statement." . What's the problem here?
 
GetEvents() is a method you write (yes you will write code, it's not all drag/drop). You create it where you need it. along those same lines you will create a class called Events. the syntax is .net 3.0 using the var keyword and auto backing fields. maybe the translator cannot handle that. I also don't know if VB supports the keyword yield (deferred execution).

all this is OOP 101 and can be done in any language/platform. the syntax and implementations may vary, but the concepts are universal.

since you are new to webforms/asp.net. understand there is a difference. webforms is a html engine. asp.net is an http framework that services request/response. webforms requires asp.net, but asp.net does not require webforms. if you don't want to use webforms you can also use Monorail or MS MVC to control your server logic. with one of these frameworks you can also choose your html engine aspview, nvelocity, webforms, brail, spark. i'm getting way off topic though.

back on point. what you need to do is not complicated, you just need to understand how to use the tool you have chosen.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top