Populate DropDownList from XmlDataSource

后端 未结 3 635
囚心锁ツ
囚心锁ツ 2021-01-21 20:06

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



  foo

        
相关标签:
3条回答
  • 2021-01-21 20:26

    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()
    );
    
    0 讨论(0)
  • 2021-01-21 20:30

    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.

    0 讨论(0)
  • 2021-01-21 20:31

    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.

    0 讨论(0)
提交回复
热议问题