【问题标题】:Create web form from xml in asp.net mvc在 asp.net mvc 中从 xml 创建 Web 表单
【发布时间】:2011-06-02 17:58:29
【问题描述】:

我被分配到某个项目的 web 部分。这个项目有两个部分, 窗口和网页。在窗口部分,用户可以创建自己的自定义模板 像visual studio IDE一样,你可以添加表单和其他控件。然后,我们保存这个模板 使用 xml 文件中的数据。我的职责是从此 xml 文件中读取并创建 web 表单。 对于 Web 部件,仅显示从窗口部件创建的信息。 我们的xml文件格式如下。对于web部分,我们用c#.net用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>

我们知道它不应该像窗口部分那样创建 web 表单,但是,我们真的需要它。 那么,我如何解决我的问题?我可以使用 xml 序列化吗? 请用一些例子给我正确的方法。

问候

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    您可以使用 XSLT 将此 XML 转换为 XHTML。此外,您可以从 MVC ActionResult 类继承您自己的 XMLBasedFormResult 并使用 C#(例如使用 LinqToXML)在 ExecuteResult 方法中生成表单 HTML。

    【讨论】:

    • 你能给我一些例子吗,拜托。非常感谢你的回答。我想试试。
    【解决方案2】:

    嗯,你不认为你最终会复制 Visual Studio 所做的事情吗?

    我建议您使用 WPF 之类的东西,它可以在浏览器和 Windows 应用程序中显示。经验告诉我,这种解析xml生成UI的思路不是很好。。几乎就像写一个jsp/asp引擎一样。

    我还注意到,您拥有的 xml 没有格式信息、屏幕上的位置、颜色、样式、字体等。

    您打算如何填充文本框、下拉列表、菜单等?

    【讨论】:

    • 说实话,web的整个部分都是用asp.net开发的,mvc.mine就是this.so的一种形式,所以不能改成wpf。谢谢你的回答。
    • 嗯..你仍然需要一些关于你希望这些标签、文本框格式、表格、div等的布局细节......还有它..你还需要映射windows 控件到 html 控件,因此某些高级 windows 控件可能难以在 html 页面中建模..拥有一个元模型更有意义,它不是特定于 windows 平台的..它应该更像 html..事实上,我认为从 html 页面创建 windows 窗体 UI 可能更容易,然后从基于 windows 窗体控件的元模型创建 web .. :D
    • 是的,我也对这个案例感到困惑。我认为如果我可以只显示数据就可以了,不需要创建一些控件,如下拉列表、菜单和按钮,因为 Web 部件只有查看。我不知道如何解决:(问候
    • 它只是视图,那么应该没有太大问题..这也意味着您可以将文本框等所有可编辑的内容转换为标签...禁用所有复选框和单选按钮(或显示文本框是是不是)...您可以使用xslt,但我发现这很难学习..在您的情况下可能有点过分了..您可以解析xml文件。然后在div中显示每个元素其他..您对生成这些文件的过程有任何控制权..那么您可以在其中放置一些逻辑以使您的工作更轻松..
    【解决方案3】:

    正如 mace 所说,XSLT 是一个很好的解决方案。

    这是 xls 样式表:

    <?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>
    

    你可以阅读这个Tutorial来获得xls的基本技能。

    您现在可以在 C# 中运行此 xsl 转换:

    //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() ;
    

    您必须针对您的场景进行修改。查看 MSDN 以获取有关 XslTransform 类等的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      相关资源
      最近更新 更多