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

Renaming a branch while on pull request

git github pull-request

On Github, you can make pull requests to add functionality to a project. One's contributions have to be on a branch that, if the request is accepted, will be merged into the master branch (or an

Given URL is not allowed by the Application configuration

facebook login facebook-login sign

I am trying to create facebook sign-in page according to this tutorial. I only changed the two lines

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.