0

I've planned a new Application. My Idea is, to generate XML docs. I need a way to convert this XML docs to a HTML Table.

My XML structure is:

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

<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. With a foreach i'll try to generate the 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!

1
  • 1
    use xpath or xlst tranformation. Commented Mar 12, 2014 at 8:48

3 Answers 3

1

Your best bet would be to use Extensible Stylesheet Language (XSL). You can use XSL Tansformations (XSLT) to create templates in xml which sets out rules on how to convert XML into another format. 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#.

Sign up to request clarification or add additional context in comments.

Comments

0

There's a standard way of doing this:

http://www.w3schools.com/xsl/

See this example for MVC integration

Comments

0

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.

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). 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:

@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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.