从后面的代码创建动态html

时间:2013-07-29 12:37:59

标签: c# html .net xml

我有一个项目得到xml,它代表一个关于主题和内部问题非常动态的问卷(问题的类型是相同的)。

我创建了一个获取xml并将其解析为我的.net对象的对象。

下一步是构建aspx页面(在.net framework 4.0中)作为对象,这意味着HTML页面也必须是动态的。 我正在尝试使用占位符,但这不是最佳方式。

有没有人有不同的想法?

示例xml:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Questionnaire>
  <clientID>1036</clientID>
  <QuestForm Desc="דוגמא" ID="1" Info="הסבר לגבי השאלון לדוגמא">
    <Subject Desc="כללי" ID="1" Info="הסבר לנושא כללי" NP="N" Num="1" TableRows="3">
      <Title Desc="מופע 1" ID="1"></Title>
      <Title Desc="מופע 2" ID="2"></Title>
      <Title Desc="מופע 3" ID="3"></Title>
      <Question Desc="טקסט חופשי" ID="1" Type="ט"></Question>
      <Question Desc="תאריך" FromDate="00/00/0000" ID="2" ToDate="31/12/2999" Type="ת"></Question>
      <Question Desc="שעה" FromTime="00:00:00" ID="3" ToTime="24:00:00" Type="ש"></Question>
      <Question Desc="מספר" FromN="1.00" ID="4" IntN="0.00" ToN="10.00" Type="מ"></Question>
      <Question Desc="בחירה" ID="5" Type="ב">
        <Answer Desc="תשובה 1" ID="1"></Answer>
        <Answer Desc="תשובה 2" ID="2"></Answer>
      </Question>
      <Question Desc="שפות דיבור בחירה מרובה" ID="6" Type="ר">
        <Answer Desc="עברית" ID="1"></Answer>
        <Answer Desc="אנגלית" ID="2"></Answer>
        <Answer Desc="רוסית" ID="3"></Answer>
      </Question>
    </Subject>
    <Subject Desc="חו&quot;ל" ID="2" Info="הסבר לנושא חול" NP="N" Num="2">
      <Question Desc="נסיעות לחול" FromN="0.00" ID="1" IntN="0.00" ToN="0.00" Type="מ"></Question>
      <Question Desc="מדינות מועדפות" ID="2" Type="ר">
        <Answer Desc="ארצות הברית" ID="1"></Answer>
        <Answer Desc="אנגליה" ID="2"></Answer>
        <Answer Desc="אוסטרליה" ID="3"></Answer>
        <Answer Desc="רוסיה" ID="4"></Answer>
      </Question>
    </Subject>
    <Subject Desc="נושא חברתי" ID="3" NP="N" Num="3">
      <Question Desc="כמה חברים יש לך" FromN="1.00" ID="1" IntN="0.00" ToN="100.00" Type="מ"></Question>
      <Question Desc="באיזה רשת חברתית אתה משתמש" ID="2" Type="ר">
        <Answer Desc="FACEBOOK" ID="1"></Answer>
        <Answer Desc="LINKEDIN" ID="2"></Answer>
        <Answer Desc="TWITER" ID="3"></Answer>
      </Question>
      <Question Desc="נולדת בארץ" ID="3" Type="ל"></Question>
      <Question CondID="3" Desc="ישוב לידה" ID="4" Type="ט"></Question>
    </Subject>
  </QuestForm>

  <Muamad Email="ORANI4U@GMAIL.COM" Event="28" ID="36130" Name="א אורן"></Muamad>

</Questionnaire>


 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                XDocument inputXml = new XDocument();
                string xmlParse = File.ReadAllText(@"C:\XmlInput\0022.xml");
                inputXml = XDocument.Parse(xmlParse);
                List<Questionnaire> questionnare = new List<Questionnaire>();
                questionnare = creator.BuildQuestionnaire(inputXml);

                dlSubjects.DataSource = questionnare.FirstOrDefault().Subjects;
                dlSubjects.DataBind();
            }
        }

        protected void dlSubjects_DataBinding(object sender, EventArgs e)
        {
            List<QuestionnnaireSubject> subjects = (List<QuestionnnaireSubject>)dlSubjects.DataSource;

            foreach (var s in subjects)
            {
                foreach (QuestionnaireQuestion q in s.Questions)
                {
                    switch (q.Type)
                    {
                        case QuestionType.Boolean:
                            CreateBooleanQ(q);
                            break;
                        case QuestionType.Date:
                            //questions.Add(CreateDateQ(element));
                            break;
                        case QuestionType.MultipleChoice:
                            //questions.Add(CreateTimeQ(element));
                            break;
                        case QuestionType.Number:
                            //questions.Add(CreateNumberQ(element));
                            break;
                        case QuestionType.SingleChoice:
                            //questions.Add(CreateChoiceQ(element));
                            break;
                        case QuestionType.Text:
                            CreateTextQ(q);
                            break;
                        case QuestionType.Time:
                            //questions.Add(CreateLogicQ(element));
                            break;
                        case QuestionType.UnKnown:
                            //questions.Add(CreateTextQ(element));
                            break;
                        default:
                            break; 
                    }
                }
            }
        }

        protected void dlSubjects_ItemDataBound(object sender, DataListItemEventArgs e)
        {

        }

        public void CreateBooleanQ(QuestionnaireQuestion q)
        {
            Label lbl = new Label();
            lbl.Text = q.Question;
            RadioButtonList rb = new RadioButtonList();
            //rb.DataMember = q.
            PlaceHolder ph = new PlaceHolder();
            ph.ID = "phBoolean";

            ph.Controls.Add(lbl);
            ph.Controls.Add(rb);
        }

        public void CreateTextQ(QuestionnaireQuestion q)
        {
            Label lbl = new Label();
            lbl.Text = q.Description;
            PlaceHolder ph = new PlaceHolder();
            ph.ID = "phText";

            ph.Controls.Add(lbl);
        }

请注意,输入xml目前用于调试和构建页面。

我刚刚启动了代码,所以我认为没有很多代码需要比较。

0 个答案:

没有答案