Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

populate dropdownlist from xml file

I have created a simple xml file that includes a small list of values to populate a dropdown list.  I can map to the xml file read the data in the xml file and bind it to the dropdownlist.

Here is my question.  I have a class library that I have created a dataset (.xsd) file with tableadapters that make calls to the database.

So, is there a way to add the dataset that pulls info from my xml file into the dataset in my class library?

I'm not sure how to do this?  I want to keep all the data extraction seperate from the application.  The way I have it now is the page code-behind:

protected void BindList()
    {
        string path = MapPath("~/products.xml");
        DataSet dSet = new DataSet();
        dSet.ReadXml(path);

        chklst.DataSource = dSet;
        chklst.DataTextField = "name";
        chklst.DataValueField = "productid";
        chklst.DataBind();
    }

Is it possible to move the dataset portion into the class library?  If so, how?

Basically, what I'd like to have in the code-behind is the following:

GetEmployeeTableAdapter employee = new GetEmployeeTableAdapter();
ddlEmployee.DataSource = employee.GetEmployee();
ddlEmployee.DataTextField = "customer_id";
ddlEmployee.DataValueField = "recordID";
ddlEmployee.DataBind();
ddlEmployee.Items.Insert(0, new ListItem("Please select...", ""));

I just don't know how to create the dataset in the class library that reads from the xml.  The table adapters only seem to connect to a database.

Is what I want to accomplish possible?

Thanks for any help.
Avatar of Sachintana Dissanayake
Sachintana Dissanayake
Flag of New Zealand image

You can use xsd to class generation command line tool and it will generate .cs classes for your xsd.
     eg. xsd.exe "schemefile.xsd" /classes

If you are looking for a solution for your whole project, you can use nHibernate to map database objects to classes. It will automatically generate .net classes by accessing the database tables.
http://www.hibernate.org/
Avatar of -Dman100-

ASKER

Hi sachintana,
Thanks for replying to my post.  My apologies, I got a little lost on your post.  I've never used the xsd to class generation tool

I might be misuderstanding, but after thinking about it further, can I not add a new class to my library project, add the xml file, create a method that creates the dataset and reads the xml data, recompile, add the reference to my web project and then simply call the method to bind the dropdownlist with the xml data?

I've heard about nHibernate, but have never used that either.  Does that work with xml files as well as databases?

Sorry for the novice questions.

I appreciate the help.  thank you.
ASKER CERTIFIED SOLUTION
Avatar of Sachintana Dissanayake
Sachintana Dissanayake
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial