Wednesday, December 9, 2009

Data Collection Form design in ASP.NET (Input designer XML which can used for CodeSmith template)

Here is the simple code which will create the Form Designer XML in the format shown below which can be used for CodeSmith template to create .ascx and .ascx.cs files.


XML:
<FormDefinition xmlns="http://aas.com.au/FormGen">
  <PageName>zddas</PageName>
  <FormId>asdas</FormId>
  <Sections>
    <Section>
      <SectionTitle>Test Section</SectionTitle>
      <Controls>
        <Control Type="text" Id="txtField" Text="txtField" />
      </Controls>
    </Section>
  </Sections>
</FormDefinition>





Code:
void GenerateInputForCodeSmith()
        {
            XmlDocument doc = new XmlDocument();
            XmlElement rootEle = default(XmlElement);
            XmlElement elt = default(XmlElement);
            XmlElement eltChoice = default(XmlElement);
            XmlAttribute attr = default(XmlAttribute);

            doc.LoadXml("<FormDefinition xmlns=\"http://aas.com.au/FormGen\"></FormDefinition>");
           
            rootEle = doc.CreateElement("PageName");
            rootEle.InnerText = txtPageName.Text;
            doc.DocumentElement.AppendChild(rootEle);

            rootEle = doc.CreateElement("FormId");
            rootEle.InnerText = txtForm.Text;
            doc.DocumentElement.AppendChild(rootEle);

            rootEle = doc.CreateElement("Sections");
            doc.DocumentElement.AppendChild(rootEle);

            XmlElement elt1 = doc.CreateElement("Section");
            rootEle.AppendChild(elt1);

            elt = doc.CreateElement("SectionTitle");
            elt.InnerText = "Test Section";
            elt1.AppendChild(elt);

            XmlElement eltControls = doc.CreateElement("Controls");
            elt1.AppendChild(eltControls);

            Panel p = (Panel)this.form1.FindControl("pnlSection");
            IEnumerable<Section> sections = p.Controls.OfType<Section>();
            Section sec = sections.First<Section>();
            IEnumerable<Field> fields = sec.FindControl("pnlField").Controls.OfType<Field>();
            foreach (Field field in fields)
            {
                elt = doc.CreateElement("Control");
                attr = doc.CreateAttribute("Type");
                attr.InnerText = ((DropDownList)field.FindControl("cboFieldType")).SelectedValue;
                elt.Attributes.Append(attr);
                attr = doc.CreateAttribute("Id");
                attr.InnerText = ((TextBox)field.FindControl("txtFieldName")).Text;
                elt.Attributes.Append(attr);
                attr = doc.CreateAttribute("Text");
                attr.InnerText = ((TextBox)field.FindControl("txtFieldName")).Text;
                elt.Attributes.Append(attr);
                string[] options = ((TextBox)field.FindControl("txtValues")).Text.Split(";".ToCharArray());
                switch (((DropDownList)field.FindControl("cboFieldType")).SelectedValue)
                {
                    case "dropdown":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("option");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;

                        }
                    case "radio":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("choice");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;
                        }
                    case "checkbox":
                        {
                            foreach (string option in options)
                            {
                                eltChoice = doc.CreateElement("choice");
                                attr = doc.CreateAttribute("value2");
                                attr.InnerText = option;
                                eltChoice.InnerText = option;
                                eltChoice.Attributes.Append(attr);
                                elt.AppendChild(eltChoice);
                            }
                            break;
                        }
                    default:
                        break;
                }
                eltControls.AppendChild(elt);
            }

            //doc.Save(Server.MapPath("FormDesigner.xml"));
            string output = doc.OuterXml;
            output = output.Replace(" xmlns=\"\"","");
            FileStream fileStream = File.Open(Server.MapPath("FormDesigner.xml"), FileMode.Create);
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] bytes = encoding.GetBytes(output);
            fileStream.Write(bytes, 0, bytes.Length);
            fileStream.Close();
            fileStream.Dispose();

        }

No comments:

Post a Comment