locked
databinding - XmlNodeList to DropDownList RRS feed

  • Question

  • User192178519 posted
    hello,

    i am attempting to do some databinding of an XmlNodeList to a DropDownList. id like to avoid looping thru my nodelist and hoped to do a simple .DataSource/.DataBind technique.

    for simplicity, lets say this is my xml:
    <people>
        <person name="Joe">[other child nodes w/ innertext]</person>
        <person name="Bob">[other child nodes w/ innertext]</person>
        <person name="Tim">[other child nodes w/ innertext]</person>
    </people>

    ...and this is my code-behind:
    'get person nodes
    Dim personNodes As XmlNodeList = xml.SelectNodes("//person")

    'bind to list
    ddlYear.DataSource =
    personNodes
    'ddlYear.DataTextField = ??
    ddlYear.DataBind()


    my desired result is a dropdownlist of items w/ text of:
    Joe
    Bob
    Tim
    ...how does one do this?


    thanks!
    matt

    Thursday, June 16, 2005 2:52 PM

All replies

  • User192178519 posted
    ...well, i dont think its possible.

    the DropDownList's .DataTextField() and .DataValueField() methods are currently not robust enough to support anything other than selecting a result-set's column names. which is pretty lame, as i hate enumerating thru lists & adding items manually -- which i find myself doing quite a bit.
     
    it would be nice if those methods supported:

       1) an index value, for when a dimensional object is set as the .DataSource()
       2) XPATH support, for when an XML object is set as the .DataSource()

    seeing as XML is god in .NET (its behind every dataset), #2 atleast would seem to be a no-brainer.

    2.0, anyone??


    thanks,
    matt

    --
    Matt Del Vecchio
    Friday, June 24, 2005 2:52 PM
  • User1613912708 posted
    I have the same problem as you. But for my case, I want to get the elements to be displayed in the DropDownList once the page is loaded.
    For example, I have <Title> asf </Title>

    I want to get "asf" to be displayed in the DropDownList that I have created. How do i do that?
    Saturday, July 2, 2005 11:56 PM
  • User-2044379205 posted
    I have the same problem as you. But for my case, I want to get the elements to be displayed in the DropDownList once the page is loaded.
    For example, I have <Title> asf </Title>

    I want to get "asf" to be displayed in the DropDownList that I have created. How do i do that?


    I have posted the solution in this post

    Hope it helps...
    Sunday, July 3, 2005 5:18 AM
  • User192178519 posted
    that's sort-of a solution.
     
    imo, looping thru a nodelist and manually adding items to a dropdownlist is not a good solution, as it's not true .net databinding.

    and populating the xml into a dataset and then binding that to the dropdownlist works, but it's a heavier solution, due to the dataset.

    ideally, webform databinding should support XPATH queries on xml nodelists, for the DataText and DataValue properties. i bet they will in time.

    i just took a look at Laszlo -- this is cool UI technology. (im reminded of it due to its standard XPATH support for databinding). take a look: http://www.openlaszlo.org/

    matt
    Tuesday, July 5, 2005 12:51 PM
  • User-903513691 posted
    Well for now you can try this...

    <people>
        <person
    name="Joe">[other child nodes w/ innertext]
    </person>
        <person
    name="Bob">[other child nodes w/ innertext]
    </person>
        <person
    name="Tim">[other child nodes w/ innertext]
    </person>
    </people>


    XmlNodeList nlst = dom.DocumentElement.SelectNodes("person[@name]/@name");


    ddl.DataSource = nlst;
    ddl.DataTextField = "InnerText";
    ddl.DataBind();

    Monday, July 11, 2005 10:36 AM
  • User-798942586 posted

    Excellent! Even after 8 years, the "InnerText" solution made my day Smile

    Tuesday, November 5, 2013 2:53 AM