将 XML 转换为 HTML 表单

本文关键字:HTML 表单 转换 XML | 更新日期: 2023-09-27 18:33:57

是否可以从XML文档在HTML中创建表单?

例如,我有以下 XML:

 <record>
    <IsEnrolled>Y</IsEnrolled>
    <IsGraduating>N</IsGraduating>
    <StudentLevel>Junior</StudentLevel>
    <StudentType>InState</StudentType>
    <HasScholarship>N</HasScholarship>
    <Action>CodeA</Action>
  </record>
  <record>
    <IsEnrolled>Y</IsEnrolled>
    <IsGraduating>N</IsGraduating>
    <StudentLevel>Sophomore</StudentLevel>
    <StudentType>InState</StudentType>
    <HasScholarship>Y</HasScholarship>
    <Action>CodeB</Action>
  </record>
  <record>
    <IsEnrolled>Y</IsEnrolled>
    <IsGraduating>N</IsGraduating>
    <StudentLevel>Freshmen</StudentLevel>
    <StudentType>OutOfState</StudentType>
    <HasScholarship>Y</HasScholarship>
    <Action>CodeC</Action>
  </record>
  <record>
    <IsEnrolled>Y</IsEnrolled>
    <IsGraduating>Y</IsGraduating>
    <StudentLevel>Senior</StudentLevel>
    <StudentType>International</StudentType>
    <HasScholarship>Y</HasScholarship>
    <Action>CodeD</Action>
  </record>

我想从上面的XML创建一个HTML表单:

<body>
    <table>
        <tr>
            <td>IsEnrolled</td>        
            <td>
                <select>
                    <option>Yes</option>
                    <option>No</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>IsGraduating</td>        
            <td>
                <select>
                    <option>Yes</option>
                    <option>No</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>StudentLevel</td>
            <td>
                <select>
                    <option>Freshmen</option>
                    <option>Sophomore</option>
                    <option>Junior</option>
                    <option>Senior</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>StudentType</td>
            <td>
                <select>
                    <option>InState</option>
                    <option>OutOfState</option>
                    <option>International</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>HasScholarship</td>
            <td>
                <select>
                    <option>Yes</option>
                    <option>No</option>
                </select>
            </td>
        </tr>
    </table>
        <span id="Action">Display the Action here based on the selected answers above</span>
</body>

XML 中的操作元素是问题的输出。我想根据在 HTML 表单中选择的内容在元素中显示此输出。

这可能吗?如果是这样,我应该如何处理?

将 XML 转换为 HTML 表单

由于我们没有太多细节,因此这是获取所需内容的起点; 我们可以想象一个假设的索引操作:

public ActionResult Index()
        {

这是您的xml字符串,如果没有根节点,则可以轻松地动态添加它

          var xmlInput = @"<?xml version=""1.0"" ?><CustomData><Records><record>
                        <IsEnrolled>Y</IsEnrolled>
                        <IsGraduating>N</IsGraduating>
                        <StudentLevel>Junior</StudentLevel>
                        <StudentType>InState</StudentType>
                        <HasScholarship>N</HasScholarship>
                        <Action>CodeA</Action>
                      </record>
                      <record>
                        <IsEnrolled>Y</IsEnrolled>
                        <IsGraduating>N</IsGraduating>
                        <StudentLevel>Sophomore</StudentLevel>
                        <StudentType>InState</StudentType>
                        <HasScholarship>Y</HasScholarship>
                        <Action>CodeB</Action>
                      </record>
                      <record>
                        <IsEnrolled>Y</IsEnrolled>
                        <IsGraduating>N</IsGraduating>
                        <StudentLevel>Freshmen</StudentLevel>
                        <StudentType>OutOfState</StudentType>
                        <HasScholarship>Y</HasScholarship>
                        <Action>CodeC</Action>
                      </record>
                      <record>
                        <IsEnrolled>Y</IsEnrolled>
                        <IsGraduating>Y</IsGraduating>
                        <StudentLevel>Senior</StudentLevel>
                        <StudentType>International</StudentType>
                        <HasScholarship>Y</HasScholarship>
                        <Action>CodeD</Action>
                      </record></Records></CustomData>";

以下是对象到 XML 的表示形式:

[XmlTypeAttribute(AnonymousType = true)]
        public class CustomData
        {
            [XmlArray(ElementName = "Records")]
            [XmlArrayItem(ElementName = "record")]
            public List<Record> Records { get; set; }
            public CustomData()
            {
                Records = new List<Record>();
            }
        }
        public class Record
        {
            [XmlElement(ElementName = "IsEnrolled")]
            public string IsEnrolled { get; set; }
            [XmlElement(ElementName = "IsGraduating")]
            public string IsGraduating { get; set; }
            [XmlElement(ElementName = "StudentLevel")]
            public string StudentLevel { get; set; }
            [XmlElement(ElementName = "StudentType")]
            public string StudentType { get; set; }
            [XmlElement(ElementName = "HasScholarship")]
            public string HasScholarship { get; set; }
            [XmlElement(ElementName = "Action")]
            public string Action { get; set; }
        }

此时,您只需要反序列化 XML:

            var serializer = new XmlSerializer(typeof(CustomData));
            CustomData data;
            using (TextReader reader = new StringReader(xmlInput))
            {
                data = (CustomData)serializer.Deserialize(reader);
            }

。并将其返回到您的视图中

            return View(data);
     }

。现在,您只需要渲染模型