Vidar
Vidar

Reputation: 6673

Smart way to create a form to generate XML based on XSD

I want to some dynamic control generation based on an XSD or DTD to be displayed on a form (WinForms) and the user can fill in values, probably mostly textboxes that will fill in attribute data and create legitimate fragments of XML to be inserted into an already existing XML file.

Is this doable - has anyone done this kind of thing before - if so what sort of design did you use?

Below is an example of the XSD:

 <xs:element name="layer-config">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="dynamic-feature-layer"/>
        <xs:element ref="dynamic-image-layer"/>
        <xs:element ref="folder"/>  
        <xs:element ref="layer"/>
        <xs:element ref="wms-layer"/>
      </xs:choice>
      <xs:attribute name="upload-can-drill-identify" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-extract-excel" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-extract-gml" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-extract-kml" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-find-by-attribute" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-hide-labels" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-identify" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-select" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-select-by-attribute" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-symbolize" type="BOOLEAN_TYPE" use="optional" default="true"/>
      <xs:attribute name="upload-can-symbolize-by-attribute" type="BOOLEAN_TYPE" use="optional" default="true"/>
    </xs:complexType>
  </xs:element>

I am thinking of doing this because I don't want to hard code rigidly to an already existing XML file in case it gets dropped or changed for something else - then you have to recode and recompile the whole thing and start again. It's also not that maintainable for end users either.

Upvotes: 2

Views: 387

Answers (1)

Adriano Carneiro
Adriano Carneiro

Reputation: 58635

Yes, this is doable. Here are a few pointers:

  • You can use LinqToXML to read the definition file (XSD), with the important properties (type, use, etc.)
  • Having your definitions in your hands, you should implement a class responsible for creating the fields
    • This class must have one method that creates one field based on one definition, using the important properties
    • This class must be able to group fields properly, according to the elements grouping in the definitions
  • You must decide what approach to use regarding positioning. There's no positioning information in the definition file and you have not reported whether this is WinForms, WebForms, Silverlight,etc.

Upvotes: 1

Related Questions