Create Survey Data Object


Zodiac.NET provides a collection of classes and APIs to create and manipulate surveys and responses data objects. For instance, you can create new survey objects and manipulate its data from your application code. This would be helpful when you try to integrate the survey engine with other external sources of the survey data according to some specific business logic.


The sample below show how to create a new survey object with title and description.

// Create new Survey object
Survey survey = new Survey("SRV-1", 
            "Sample Survey", "Survey Description", 
            "Thank You!", "Thanks for Taking this survey");

The wizard steps of the survey is called: Pages. You can add new pages to the survey object as following:

// Create new Survey Pages
SurveyPage page1 = new SurveyPage("PG-1", "Page Title 1", "Page Description 1");
SurveyPage page2 = new SurveyPage("PG-2", "Page Title 2", "Page Description 2");

After creating the survey pages, you will need to create the question objects to be added to the survey pages. The following is a sample for creating a single choice question and assigning it to the one of the survey pages.

// Create new Single Choice Question
SingleChoiceQuestion singleChoiceQuestion =
            new SingleChoiceQuestion("SQ-1" /* Question ID */, 
            "What is your selected choice?" /* Question Body */);
singleChoiceQuestion.Choices.Add("Choice 1");
singleChoiceQuestion.Choices.Add("Choice 2");
singleChoiceQuestion.Choices.Add("Choice 3");

singleChoiceQuestion.Page = page1;

The following is another sample to create a new text question and assign it to another survey page.

// Create new Text Question
TextQuestion textQuestion = new TextQuestion("TQ-1" /* Question ID */,
            "What is your email address?" /* Body */, 
            "Enter your mail address below" /* Remarks */,  
            true /* Mandatory? */, 
            "This is mandatory question" /* Footer*/, 
            QuestionFormat.Email /* Validation Format */);

textQuestion.Page = page2;

You can add the questions to the survey pages as following:

page1.Questions.Add(singleChoiceQuestion);
page2.Questions.Add(textQuestion);

survey.Pages.Add(page1);
survey.Pages.Add(page2);

You can also serialize the survey data object using the XML Serialization class: SurveyXmlSerializer. This would be useful to save the XML representation of the survey in your database or any external resource.

// Serialize the survey object to XML
string xml = SurveyXmlSerializer.SerializeSurveyToXml(survey);

Complete Sample Code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using MentorLogic.Engines.Zodiac;
using MentorLogic.Engines.Zodiac.Serialization;
using MentorLogic.Engines.Zodiac.Repository;

public partial class Builder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create new Survey object
        Survey survey = new Survey("SRV-1", 
            "Survey Tite", "Survey Description", 
            "Thank You!", "Thanks for Taking this survey");

        // Create new Survey Pages
        SurveyPage page1 = new SurveyPage("PG-1", "Page Title 1", "Page Description 1");
        SurveyPage page2 = new SurveyPage("PG-2", "Page Title 2", "Page Description 2");

        // Create new Single Choice Question
        SingleChoiceQuestion singleChoiceQuestion =
            new SingleChoiceQuestion("SQ-1" /* Question ID */, 
            "What is your selected choice?" /* Question Body */);
        singleChoiceQuestion.Choices.Add("Choice 1");
        singleChoiceQuestion.Choices.Add("Choice 2");
        singleChoiceQuestion.Choices.Add("Choice 3");

        singleChoiceQuestion.Page = page1;

        // Create new Text Question
        TextQuestion textQuestion = new TextQuestion("TQ-1" /* Question ID */,
            "What is your email address?" /* Body */, 
            "Enter your mail address below" /* Remarks */,  
            true /* Mandatory? */, 
            "This is mandatory question" /* Footer*/, 
            QuestionFormat.Email /* Validation Format */);

        textQuestion.Page = page2;

        page1.Questions.Add(singleChoiceQuestion);
        page2.Questions.Add(textQuestion);

        survey.Pages.Add(page1);
        survey.Pages.Add(page2);
         
        // Serialize the survey object to XML
        string xml = SurveyXmlSerializer.SerializeSurveyToXml(survey);
        

    }
}


See Also