如何在MVC 4表中显示Xml-Xdocument

本文关键字:显示 Xml-Xdocument MVC | 更新日期: 2023-09-27 17:57:55

我需要在我的mvc应用程序的表中显示任何xml,就像在照片中一样

https://drive.google.com/file/d/0B9JoyO0mTmhnNWplMlR6ME5PeW8/edit?usp=sharing

这是我的代码,但它只适用于xml中的一个父元素

enter code here String f1 = Session["doc2"].ToString();
        Value model2 = new Value();

        HttpPostedFileBase file2 = TempData["doc3"] as HttpPostedFileBase;
        var fileex = Path.GetExtension(file2.FileName);
        string xmlstr = ".xml"; 
        string strall = "";
        XDocument xml = XDocument.Parse(f1);
             var abc = xml.Root.DescendantNodes().OfType<XElement>()
                                               .Select(x => x.Name).Distinct();
            ViewBag.MyListabc = abc;
            var abcd = xml.Root.DescendantNodes().OfType<XElement>()
                                                   .Select(x => x.Value).Distinct();
            ViewBag.MyListabcd = abcd;

        model2 = new Value()
        {
            file1 = abc
        };

        //ViewBag.MyList = listelem;
        return View(model2);

    }

这是我的观点

enter code here<table style="font-family: verdana,arial,sans-serif;
font-size:11px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;">

@foreach (var item in ViewBag.MyListabc) {

                   <th style="border: 1px solid; width:80px">   @Html.DisplayTextFor(x => item) </th>
}</tr>

@foreach (var item in ViewBag.MyListabcd) { <td style="border: 1px solid; width:80px"> @Html.DisplayTextFor(x => item) </td>

}</tr>
</table>

如何在MVC 4表中显示Xml-Xdocument

您可以将xml表解析为DataTable对象,然后在视图中呈现DataTable。一些灵感链接。

  • 在MVC中显示标准数据表
  • http://weblogs.asp.net/gunnarpeipman/archive/2011/11/19/asp-net-mvc-simple-view-to-display-contents-of-datatable.aspx