[英]Create HTML Table with XML in a asp.net MVC C# Application
I've planned a new Application. 我已经计划了一个新的应用程序。 My Idea is, to generate XML docs.
我的想法是生成XML文档。 I need a way to convert this XML docs to a HTML Table.
我需要一种将XML文档转换为HTML表的方法。
My XML structure is: 我的XML结构是:
<Checklist>
<Title>Titletext</Title>
<Group>
<Title>Active Directory</Title>
<Content>
<Line>
<text type="array">
<value>Connect to:</value>
<value>dsa.msc start</value>
</text>
</Line>
<Line>
<text type="array">
<value>Gruppen anpassen anhand des Arbeitsortes</value>
<value>Profilpfad eintragen</value>
</text>
</Line>
</Content>
</Group>
</Checklist>
I'll try to convert this xml to HTML Tables like this: 我将尝试将此xml转换为HTML表,如下所示:
<html>
<table>
<tr class="head">
<td>#Group -> Title</td>
</tr>
<tr class="text">
<td><p>#Line -> Value 1</p><p>@Line -> Value2</p></td>
</tr>
</table>
</html>
My first idea was, to read the XML line by line, and add this values to a ListArray. 我的第一个想法是,逐行读取XML,并将此值添加到ListArray。 With a foreach i'll try to generate the HTML
使用foreach,我将尝试生成HTML
foreach(string item in ViewBag.Content)
Is there a much "better" option or should i try to solve this this way =) Maybe someone can give me a best practice hint or something =) 有很多“更好”的选择,还是我应该尝试通过这种方式解决此问题=)也许有人可以给我一个最佳实践的提示或其他东西=)
Thanks! 谢谢!
Your best bet would be to use Extensible Stylesheet Language ( XSL ). 最好的选择是使用可扩展样式表语言( XSL )。 You can use XSL Tansformations ( XSLT ) to create templates in xml which sets out rules on how to convert XML into another format.
您可以使用XSL Tansformations( XSLT )在xml中创建模板,该模板设置了有关如何将XML转换为另一种格式的规则。 In your case the template would be something like:
在您的情况下,模板将类似于:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th><xsl:value-of select="Checklist/Title/Group/Title" /></th>
</tr>
<xsl:for-each select="Checklist/Group/Content/Line">
<tr>
<td>
<xsl:for-each select="Value">
<p><xsl:value-of select="value" /></p>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Here's a good example on W3Schools and this SO answer shows you how to implement it in C#. 这是W3Schools上的一个好例子, 这个SO答案向您展示了如何在C#中实现它。
There's a standard way of doing this: 有一种标准的方法可以做到这一点:
http://www.w3schools.com/xsl/ http://www.w3schools.com/xsl/
See this example for MVC integration 请参阅此示例以进行MVC集成
Firstly, I'd create a class that represents a row of your table - a model. 首先,我将创建一个代表表的一行的类-一个模型。
Than I would use LINQ to XML to convert the xml file to a list of objects of your model class. 比起我,我将使用LINQ to XML将xml文件转换为模型类的对象列表。
Than, instead of putting it to the ViewBag, I'd put the list of model objects as a View's model (it can be strongly typed). 比起将模型对象列表放置为View的模型(可以将其强类型化),而不是将其放置到ViewBag中。 In the controller you return the view this way:
在控制器中,您可以通过以下方式返回视图:
List<MyModel> items = ....
return View(items)
And finally I'd iterate through it in Razor like that: 最后,我会像这样在Razor中进行迭代:
@foreach(var item in Model){ // the Model here is whatever you passed to the View method call in your controller
// create table rows or whatever you need
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.