提取文件夹中的xml文件并显示在表中

时间:2019-04-30 22:16:04

标签: c# xml model-view-controller

我正在Visual Studio中使用MVC,试图遍历给定文件夹中的所有.xml文件,并将每个文件显示在其自己的表中。

我一直在控制器和视图中与foreach一起“玩耍”,但无法正确完成。 我要去哪里错了?

这是我当前的控制器代码。

using Grid_XML_MVC.Models;
using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using System.Diagnostics;

namespace Grid_XML_MVC.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            List<LagerstatusModel> lagerstatusheading = new List<LagerstatusModel>();
            List<LagerstatusModel> lagerstatusdata = new List<LagerstatusModel>();

            //Gå gjennom alle XML i mappen
            string path = Server.MapPath("~/XML");

            //Loop through the selected Nodes.
            foreach (var file in Directory.GetFiles(path))
            {

                //Load the XML file in XmlDocument.
                XmlDocument doc = new XmlDocument();
                doc.Load(file);

                foreach (XmlNode node in doc.SelectNodes("/Salater/Arkinfo"))
                {
                    lagerstatusheading.Add(new LagerstatusModel
                    {
                        Oppdatert = node["Oppdatert"].InnerText,
                        Arknavn = node["Arknavn"].InnerText,
                        Dag1 = node["Dag1"].InnerText,
                        Dag2 = node["Dag2"].InnerText,
                        Dag3 = node["Dag3"].InnerText,
                        Dag4 = node["Dag4"].InnerText,
                        Dag5 = node["Dag5"].InnerText,
                        Dag6 = node["Dag6"].InnerText,
                        Dag7 = node["Dag7"].InnerText
                    });
                }

                //Loop through the selected Nodes.
                foreach (XmlNode node in doc.SelectNodes("/Salater/Vare"))
                {
                    //Fetch the Node values and assign it to Model.
                    lagerstatusdata.Add(new LagerstatusModel
                    {
                        Varenr = node["Varenr"].InnerText,
                        Produkt = node["Produkt"].InnerText,
                        Enhet = node["Enhet"].InnerText,
                        Ledig_Mandag = node["Ledig_Mandag"].InnerText,
                        Ledig_Tirsdag = node["Ledig_Tirsdag"].InnerText,
                        Ledig_Onsdag = node["Ledig_Onsdag"].InnerText,
                        Ledig_Torsdag = node["Ledig_Torsdag"].InnerText,
                        Ledig_Fredag = node["Ledig_Fredag"].InnerText,
                        Ledig_Lordag = node["Ledig_Lordag"].InnerText,
                        Ledig_Sondag = node["Ledig_Sondag"].InnerText,
                        Solgt = node["Solgt"].InnerText,
                        Kommentar = node["Kommentar"].InnerText
                    });
                }
            }
            return View(lagerstatusdata);
        }
    }
}

这是我的查看代码

@using Grid_XML_MVC.Models
@model IEnumerable<LagerstatusModel>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Lagerstatus</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    <script src="~/filer/jquery.floatThead.min.js"></script>
</head>

<body>
    <nav class="navbar navbar-inverse">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Lagerstatus</a>
            </div>
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                        Avdeling
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Aakre</a></li>
                        <li><a href="#">Agder</a></li>
                        <li><a href="#">Bergen</a></li>
                        <li><a href="#">Bodø</a></li>
                        <li><a href="#">BSO</a></li>
                        <li><a href="#">BVT</a></li>
                        <li><a href="#">Hamar</a></li>
                        <li><a href="#">Stavanger</a></li>
                        <li><a href="#">Trondheim</a></li>
                        <li><a href="#">Ålesund</a></li>
                    </ul>
                </li>
                <li><a href="#">Salatfabrikken</a></li>
                <li><a href="#">Sjømat</a></li>
                <li><a href="#">Frysevarer</a></li>
                <li><a href="#">Kantsortiment</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <div class="form-group">
                    <input type="text" id="myInput" class="form-control" placeholder="Filtrer liste" title="Skriv produktnavn">
                </div>
            </form>
        </div>
    </nav>

    <div class="container">
        @foreach (LagerstatusModel lagerstatusheading in Model)
        {
            <table class="table table-hover">
                <thead>
                    <tr>
                        <td><h2>@lagerstatusheading.Arknavn</h2><h4>@lagerstatusheading.Oppdatert</h4></td>
                    </tr>
                </thead>
            </table>
            <table class="table table-hover" id='lagertabell'>
                <thead>
                    <tr>
                        <th width="70px">Varenr</th>
                        <th width="300px">Produkt</th>
                        <th width="50px">Enhet</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag1</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag2</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag3</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag4</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag5</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag6</th>
                        <th align="center" width="50px">@lagerstatusheading.Dag7</th>
                        <th align="center" width="50px">Solgt</th>
                        <th align="left">Kommentar</th>
                    </tr>
                </thead>
                <tbody id="myTable">
                    @foreach (LagerstatusModel lagerstatusdata in Model)
                    {
                        <tr>
                            <td align="left">@lagerstatusdata.Varenr</td>
                            <td>@lagerstatusdata.Produkt</td>
                            <td>@lagerstatusdata.Enhet</td>
                            <td align="center">@lagerstatusdata.Ledig_Mandag</td>
                            <td align="center">@lagerstatusdata.Ledig_Tirsdag</td>
                            <td align="center">@lagerstatusdata.Ledig_Onsdag</td>
                            <td align="center">@lagerstatusdata.Ledig_Torsdag</td>
                            <td align="center">@lagerstatusdata.Ledig_Fredag</td>
                            <td align="center">@lagerstatusdata.Ledig_Lordag</td>
                            <td align="center">@lagerstatusdata.Ledig_Sondag</td>
                            <td align="center">@lagerstatusdata.Solgt</td>
                            <td align="left">@lagerstatusdata.Kommentar</td>
                        </tr>
                    }
                </tbody>
            </table>
                    }
        </div>
</body>
</html>

0 个答案:

没有答案