locked
reading xml into dropdownlist RRS feed

  • Question

  • User-541003552 posted

    Hello All,

    How can i read a multilevel xml file into a dropdownlist control.

    Any help please.

    Thursday, November 13, 2014 1:23 PM

Answers

  • User2103319870 posted

    You can use the DateSet's ReadXml(XmlReader) method to read the data from Xml in to dataset and then populate the dropdownlist from dataset

    Sample Code

    string filePath = Server.MapPath("~/XMLFile1.xml");
                //Create Dataset to hold the data
                using (DataSet ds = new DataSet())
                {
                    //Read the data into dataset
                    ds.ReadXml(filePath);
                    //Assign the datasource to dropdownlist
                    DropDownList1.DataSource = ds;
                    //Set the datatextfield
                    DropDownList1.DataTextField = "TextField";
                    //Set the DataValueField
                    DropDownList1.DataValueField = "ValueField";
                    //Bind the dropdownlist with data
                    DropDownList1.DataBind();
                }

    Sample XML

    <?xml version="1.0" encoding="utf-8" ?>
    <rootnodes>
      <node>
        <ValueField>1</ValueField>
        <TextField>Val1</TextField>
      </node>
      <node>
        <ValueField>2</ValueField>
        <TextField>Val2</TextField>
      </node>
      <node>
        <ValueField>3</ValueField>
        <TextField>Val3</TextField>
      </node>
      <node>
        <ValueField>4</ValueField>
        <TextField>Val4</TextField>
      </node>
    </rootnodes>

    Source

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 13, 2014 1:37 PM
  • User61956409 posted

    Hi KeeEdwins,

    Thanks for your post.

    As for your problem, I agree with A2H. We usually use DataSet.ReadXml Method to read XML schema and data into the DataSet as below.

    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("UserInfo.xml"));
    

    For more information about DataSet.ReadXml Method, you coul check the following link.

    Besides, you could refer to this tutorial to read and bind XML File to ASP.Net DropDownList Control.

    If you have any question about this issue, please post back freely.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 14, 2014 3:02 AM

All replies

  • User2103319870 posted

    You can use the DateSet's ReadXml(XmlReader) method to read the data from Xml in to dataset and then populate the dropdownlist from dataset

    Sample Code

    string filePath = Server.MapPath("~/XMLFile1.xml");
                //Create Dataset to hold the data
                using (DataSet ds = new DataSet())
                {
                    //Read the data into dataset
                    ds.ReadXml(filePath);
                    //Assign the datasource to dropdownlist
                    DropDownList1.DataSource = ds;
                    //Set the datatextfield
                    DropDownList1.DataTextField = "TextField";
                    //Set the DataValueField
                    DropDownList1.DataValueField = "ValueField";
                    //Bind the dropdownlist with data
                    DropDownList1.DataBind();
                }

    Sample XML

    <?xml version="1.0" encoding="utf-8" ?>
    <rootnodes>
      <node>
        <ValueField>1</ValueField>
        <TextField>Val1</TextField>
      </node>
      <node>
        <ValueField>2</ValueField>
        <TextField>Val2</TextField>
      </node>
      <node>
        <ValueField>3</ValueField>
        <TextField>Val3</TextField>
      </node>
      <node>
        <ValueField>4</ValueField>
        <TextField>Val4</TextField>
      </node>
    </rootnodes>

    Source

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 13, 2014 1:37 PM
  • User61956409 posted

    Hi KeeEdwins,

    Thanks for your post.

    As for your problem, I agree with A2H. We usually use DataSet.ReadXml Method to read XML schema and data into the DataSet as below.

    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath("UserInfo.xml"));
    

    For more information about DataSet.ReadXml Method, you coul check the following link.

    Besides, you could refer to this tutorial to read and bind XML File to ASP.Net DropDownList Control.

    If you have any question about this issue, please post back freely.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 14, 2014 3:02 AM