将XML转换为HTML表单

时间:2014-12-11 19:24:33

标签: c# html xml asp.net-mvc

是否可以从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中的Action元素是问题的输出。我想根据HTML表单中选择的内容在元素中显示此输出。

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

1 个答案:

答案 0 :(得分:1)

由于我们没有太多细节,因此这是获得所需内容的起点;我们可以想象一个假设的指数行动:

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);
     }

...现在你只是为了渲染你的模型