Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate DropDownList from XmlDataSource

I'd like to populate my DropDownList using a simple xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Databases>
  <Database>foo</Database>
  <Database>bar</Database>
  <Database>baz</Database>
</Databases>

My XPath is

/Databases/Database

My drop down list is rendered as:

<select name="databaseDropDownList" id="databaseDropDownList"> 
                    <option selected="selected" value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
                    <option value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
                    <option value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
</select>

How should I extract the text?

Thanks

like image 927
Luca Martinetti Avatar asked Mar 22 '26 08:03

Luca Martinetti


2 Answers

I can't recall it from the top of my head but I think there was a bug in XmlDataSource that prevents you to bind to values of xml nodes. It works with attributes only. Please correct me if I am wrong with this. There's a slight modification you need to make to your XML file:

<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string xml =
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Databases>
  <Database name=""foo"" />
  <Database name=""bar"" />
  <Database name=""baz"" />
</Databases>";
            databasesSource.Data = xml;
            databasesSource.DataBind();
        }
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="databases" runat="server" DataSourceID="databasesSource" DataValueField="name" DataTextField="name" />
        <asp:XmlDataSource ID="databasesSource" runat="server" XPath="/Databases/Database" />
    </div>
    </form>
</body>
</html>

Note that I added the name attribute instead of using the value of the node directly.

If you can't modify the structure of your original XML file you can apply an XSLT transformation on it using the TransformFile property as described in this post.

like image 186
Darin Dimitrov Avatar answered Mar 23 '26 21:03

Darin Dimitrov


I had the same problem today. My solution:

This is my xml:

<?xml version="1.0" encoding="utf-8"?>

<pokemons>
  <pokemon>
    <nome itemname="bulbassaur">bulbassaur </nome>
  </pokemon>
  <pokemon>
    <nome itemname="charmander">chamander </nome>
  </pokemon>
  <pokemon>
    <nome itemname="squirtle"> squirtle </nome>
  </pokemon>
</pokemons>

And I put DataTextField="itemname" on the DropDownList server control. ex:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="XmlDataSource1" DataTextField="itemname">

It's working without problems. Probably not the best solution,... but at least better than System.Web.UI.WebControls.XmlDataSourceNodeDescriptor.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!