views:

2222

answers:

3

Hello,

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

+1  A: 

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"&gt;

<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.

Darin Dimitrov
A: 

Here is one way of doing it - you can project an array of ListItems in a LINQ query:

XDocument doc = XDocument.Parse(@"<Databases>
     <Database>foo</Database>
     <Database>bar</Database>
     <Database>baz</Database>
    </Databases>");

YourList.Items.AddRange(
    (from XElement el in doc.Descendants("Database")
    select new ListItem(el.Value)).ToArray()
);
Andrew Hare
thx but I want to "Populate DropDownList from XmlDataSource"
Luca Martinetti
A: 

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.