Steve Han
Steve Han

Reputation: 11

Create web form from xml in asp.net mvc

I am assigned to web part of some project.There are two parts in this project, window and web.In window part,User can create their own customized template like visual studio IDE,you can addform and other controls.And then,we save this template with data in xml file.My duty isto read from this xml file and create webform. For web part,just showing information that createdfrom window part. our xml file format is like below.For web part,we developed in c#.net with asp.net mvc.

<Object type="System.Windows.Forms.Form">
  <Object type="System.Windows.Forms.Label">
    <Property name="Name">lblCity</Property>
    <Property name="Text">City</Property>
  </Object>
  <Object type="System.Windows.Forms.TextBox">
    <Property name="Name">txtCity</Property>
    <Property name="Text">England</Property>
  </Object>
  <Object type="System.Windows.Forms.Label">
    <Property name="Name">lblNRIC</Property>
    <Property name="Text">NRIC</Property>
  </Object>
  <Object type="System.Windows.Forms.TextBox">
    <Property name="Name">txtNRIC</Property>
    <Property name="Text">ABC01234</Property>
  </Object>
  <Object type="System.Windows.Forms.RadioButton">    
    <Property name="Name">RadioButton1</Property>    
    <Property name="Text">OptionA</Property>    
  </Object>  
  <Object type="System.Windows.Forms.CheckBox">
    <Property name="Name">CheckBox1</Property>
    <Property name="Text">Yes</Property>
  </Object> 
  <Object type="System.Windows.Forms.CheckBox">
    <Property name="Name">CheckBox2</Property>
    <Property name="Text">No</Property>
  </Object>
  <SampleDataSet>
    <SampleTable>
      <TableName>Sample1</TableName>
      <ProductName>ABC</ProductName>
      <Price>100</Price>
      <Qty>10</Qty>
      <Amount>1000</Amount>
    </SampleTable>
    <SampleTable>
      <TableName>Sample2</TableName>
      <ProductName>DEF</ProductName>
      <Price>200</Price>
      <Qty>20</Qty>
      <Amount>4000</Amount>
    </SampleTable>
    <SampleTable>
      <TableName>Sample3</TableName>
      <ProductName>GHK</ProductName>
      <Price>300</Price>
      <Qty>30</Qty>
      <Amount>9000</Amount>
    </SampleTable>
  </SampleDataSet>
  </Object>

We know it should not be create web form like window part,but,we really need it. So,how i solve my problem?can i use xml serilization? please give me right ways with some examples.

Regards

Han

Upvotes: 0

Views: 2955

Answers (3)

Arxisos
Arxisos

Reputation: 2739

As mace said XSLT is a good solution.

This is the xls stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Template for <Object type="System.Windows.Forms.Form -->
<xsl:template match="*[@type = 'System.Windows.Forms.Form']">
    <!-- Here's the html's form tag -->
    <form>
        <fieldset>
            <xsl:apply-templates />
        </fieldset>
    </form>
</xsl:template>

<!-- Template for <Object type="System.Windows.Forms.Label -->
<xsl:template match="*[@type = 'System.Windows.Forms.Label']">
    <!-- Here's the html's label tag -->
    <label><xsl:value-of select="." /></label>  
</xsl:template>

<!-- Template for <Object type="System.Windows.Forms.TextBox -->
<xsl:template match="*[@type = 'System.Windows.Forms.TextBox']">
    <!-- Create the html's input tag -->
    <xsl:element name="input">
        <!-- And set the attributes -->
        <xsl:attribute name="name">
            <xsl:value-of select="./Property[@name ='Name']" />
        </xsl:attribute>
        <xsl:attribute name="type">text</xsl:attribute>
        <xsl:attribute name="value">
            <xsl:value-of select="./Property[@name ='Text']" />
        </xsl:attribute>
    </xsl:element>
</xsl:template>

<!-- Add here templates for RadioButton, Checkbox ... -->

</xsl:stylesheet>

You can read this Tutorial to get basic skills of xls.

You can run this xsl transformation now in C#:

//load the Xml doc
XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;

XslTransform myXslTrans = new XslTransform() ;

//load the Xsl 
myXslTrans.Load(sXslPath) ;

//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
                ("result.html", null);

//do the actual transform of Xml
myXslTrans.Transform(myXPathDoc,null, myWriter);        

myWriter.Close() ;

You have to modify that for your scenario. Look at the MSDN for information about the XslTransform-class etc.

Upvotes: 0

Mahesh
Mahesh

Reputation: 1693

Well, don't you think you will really end up duplicating what visual studio does..?

I would suggest you use something like WPF, which can be shown in the browser as well as in the Windows app. I am telling from experience this idea of parsing xml and generating UI is not very good..it is almost like writing a jsp/asp engine.

Also i noticed that the xml you have there has no formatting information, location on screen, color, style, font etc..

How do you intend on populating text boxes, dropdown lists, menus etc..?

Upvotes: 0

Victor Haydin
Victor Haydin

Reputation: 3548

You could use XSLT to transform this XML to XHTML. Also, you could inherit your own XMLBasedFormResult from MVC ActionResult class and generate form HTML at ExecuteResult method using C# (e.g. with LinqToXML).

Upvotes: 5

Related Questions