Binding an xml node's children to ASP.Net DropDownList

I'm trying to bind the contents of a node of XML to a Drop Down List without much success.

Initially, the whole XML document is bound to a repeater - this works perfectly, but now I need to display a drop down list based on the children in the "" node, but I get Data at the root level is invalid. Line 1, position 1 error message on the DataBind() method on the dropDownList.

Can anyone tell me what I'm doing wrong please??

The code snippet I'm using is:

    IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
    XPathNavigator questionNode = x.CreateNavigator();
   string question = questionNode.SelectSingleNode("questionText").ToString();

   //string title = xePage.SelectSingleNode("q").InnerText;

    Literal questionText = (Literal)e.Item.FindControl("litQuestionText");
    questionText.Text = question;
    Panel iconDiv = (Panel)e.Item.FindControl("divIcon");
    iconDiv.CssClass = string.Format("icon {0}", questionNode.SelectSingleNode("iconType"));
    Panel sliderPanel = (Panel)e.Item.FindControl("pnlSlider");
    DropDownList answerDropDown = (DropDownList)e.Item.FindControl("ddlAnswer");
    TextBox answerText = (TextBox)e.Item.FindControl("txtAnswer");

    switch (questionNode.SelectSingleNode("answerType").ToString())
    {
        case "d": 
            sliderPanel.Visible = false;
            answerText.Visible = false;
            answerDropDown.Visible = true;

            XmlDataSource answersList = new XmlDataSource();
            answersList.Data = questionNode.Select("answers").ToString();
            Response.Write(answersList.ToString());
            //XPathNodeIterator answers = questionNode.Select("answers");
            //answers.AsQueryable();
            answersList.ID = questionNode.SelectSingleNode("questionId").ToString();
            answerDropDown.DataSource = answersList;
            answerDropDown.DataTextField = "@display";
            answerDropDown.DataValueField = "@value";
            answerDropDown.DataBind();
            break;

        case "s":
            sliderPanel.Visible = true;
            answerText.Visible = false;
            answerDropDown.Visible = false;
            break;
        case "t":
            sliderPanel.Visible = false;
            answerText.Visible = true;
            answerDropDown.Visible = false;
            break;

and the XML I'm using is thus:

<questions>
  <question>
    <questionId>1</questionId>
    <questionText>Question here?</questionText>
    <iconType>a</iconType>
    <answerType>d</answerType>
    <answers>
      <answer value="-3" display="Extremely badly"/>
      <answer value="-2" display="Very badly"/>
      <answer value="-1" display="Quite badly"/>
      <answer value="0" display="Neither well nor badly"/>
      <answer value="1" display="Quite well"/>
      <answer value="2" display="Very well"/>
      <answer value="3" display="Extremely well"/>
    </answers>
  </question>
  <question>
    <questionId>1</questionId>
    <questionText>Question again here?</questionText>
    <iconType>b</iconType>
    <answerType>s</answerType>
    <answers/>
  </question>
</questions>

Answers


Apparently I'd missed out the xml declaration at the top of the xml document. Clever.

However, the best way to get this working is to create a Questionnaire object and classes for the Question and Answer and then convert the XML to the classes by deserializing it. Then you can bind the Questionnaire's questions to the drop down list and it basically works a load better and easier after the initial work you have to do to create the objects.


Need Your Help

How to do worker-to-worker communication?

javascript html5 web-worker

I'm experimenting with web workers, and was wondering how well they would deal with embarassingly parallell problems. I therefore implemented Connaway's Game of Life. (To have a bit more fun than d...

How can i define an array of char* in a struct in a separate header file?

c++ pointers struct header-files

i'm just beignning to learn about structs and seperating things into different files.

Build a Web Service API (SOAP) inside MVC app?

asp.net-mvc web-services asp.net-mvc-2 soap

Can anyone help me on how to build a web service api (soap) inside an MVC app? WCF is fine :) (initially, WCF is not an option.)

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.