databinding xml to dropdownlist

Discussion in 'ASP .Net' started by Joe Gass, Apr 29, 2005.

  1. Joe Gass

    Joe Gass Guest

    I'd like to bind some xml to a dropdownlist

    <engines>
    <engine name="test1" id="1" />
    <engine name="test2" id="2" />
    </engines>

    If I do:

    ddlEngines.DataSource = xmlDoc.SelectNodes("/engines/engine")
    ddlEngines.DataBind()

    I get
    <select name="ddlEngines" id="ddlEngines">
    <option value="System.Xml.XmlElement">System.Xml.XmlElement</option>
    <option value="System.Xml.XmlElement">System.Xml.XmlElement</option>
    </select>

    So it is binding ok, I just need to set the datatextfield and datavaluefield
    to the sttributes name and id
    I'm not sure how this is achievable
    Thanks

    Joe Gass
     
    Joe Gass, Apr 29, 2005
    #1
    1. Advertising

  2. Hi Joe,

    Welcome to ASP.NET newsgroup.
    As for the dropdownlist and xmlnodelist databinding problem you mentioned,
    this is because the DropDownList is not a templated databound control(like
    datalist ,repeater...), it's a normal databound control , the binded
    dataitem need to provide strong-typed "Property", so that we can specify
    the "DataTextField", "DataValueField" for it.
    As for your scenario, the binded datasource is a XmlNodeList which contains
    list of XmlNode type. DropDownList can directly bind with such type and
    retreive further info from the XmlNode's Attributes (or sub node....).

    I think we have the following alternative means currently:

    1. Still use DropDownList, however instead of databinding, we need to
    manually use foreach loop to add ListItems into DropDownList according to
    the XmlNodeList

    2. Still using DataBinding approach, but we need to use template databound
    control such as DataList or Repeater. For example, below is a simple
    example that use Repeater control to bind a XmlNodeList and display a
    select box:

    ======aspx=========
    <asp:Repeater id="rptXml" runat="server">
    <HeaderTemplate>
    <select id="lstXml">
    </HeaderTemplate>
    <ItemTemplate>
    <option value='<%#
    ((System.Xml.XmlNode)Container.DataItem).Attributes["id"].Value %>' >
    <%#((System.Xml.XmlNode)Container.DataItem).Attributes["name"].Value
    %></option>
    </ItemTemplate>
    <FooterTemplate>
    </select>
    </FooterTemplate>
    </asp:Repeater>


    =========code behind==========
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    BindData();
    }
    }


    private void BindData()
    {
    string xmldata = @"<engines><engine name='test1' id='1' /><engine
    name='test2' id='2' /></engines>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmldata);
    XmlNodeList nodes = doc.SelectNodes("/engines/engine");
    rptXml.DataSource = nodes;
    rptXml.DataBind();
    }

    =============================

    Hope helps. Thanks,

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 30, 2005
    #2
    1. Advertising

  3. Joe Gass

    qvo178

    Joined:
    Aug 28, 2008
    Messages:
    19
    You can do it with a few line of codes using the dataset. Try this tutorial here. http : / /w w w.itjungles.com/dropdownlist/the-easiest-way-to-populate-drop-down-list-with-xml-in-c
     
    qvo178, Feb 22, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon Shaw

    Databinding Related Dropdownlist

    Leon Shaw, Jul 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    408
    Leon Shaw
    Jul 11, 2003
  2. Simon

    DropDownList not Databinding

    Simon, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    404
    Anatoly
    Nov 19, 2003
  3. tshad
    Replies:
    8
    Views:
    2,253
    tshad
    Oct 19, 2005
  4. jobs
    Replies:
    0
    Views:
    1,717
  5. Earl Teigrob
    Replies:
    1
    Views:
    435
    Shravan
    Mar 1, 2004
Loading...

Share This Page