简体   繁体   English

如何使用MVC读取Xml文件中的子节点

[英]How do I read Child Nodes in Xml file using MVC

## XML FİLE ## ## XML文件##

<Title month="1" year="2016">
    <film day="1">
    <morning>
    Fight Club
    </morning>
    <night>
    Inceptıon
    </night>
    </film>
    <film day="2">
    <morning>
    xyzasda
    </morning>
    <night>
    czxsadasas
    </night>
    </film>
    </Title>

MY CLASS 我的课

public class FilmController : Controller
{

  public ActionResult DisplayXML()
        {
            var data = new List<Films>();
            data = ReturnData();
            return View(data);
        }

 private List<Films> ReturnData(){
                string xmldata = "myxmldata.xml";
                DataSet ds = new DataSet();
                ds.ReadXml(xmldata);
                var filmlist= new List<Films>();
                filmlist= (from rows in ds.Tables[0].AsEnumerable()
                             select new Films
                             {

                                 month= Convert.ToInt32(rows[0].ToString()),
                                 year= rows[1].ToString(),

                                 film= rows[2].ToString(),
                                 morning= rows[3].ToString(),
                                 night= rows[4].ToString(),

                             }).ToList();
                return filmlist;
    }
}

Model 模型

public int month{ get; set; }
public string year{ get; set; }
public string day { get; set; }
public string morning { get; set; }
public string night{ get; set; }

How to read child node? 如何读取子节点? I want to create a table. 我想创建一个表。 I will create a table using this data. 我将使用此数据创建一个表。 I want to keep it on a list. 我想把它放在清单上。 I edited.. Error: Additional information: Cannot find column 3. 我编辑了。错误:其他信息:找不到列3。

where is the error? 错误在哪里? I want to read the xml file. 我想阅读xml文件。

You can retrieve Films collection using Linq-To-XML easily like this: 您可以使用Linq-To-XML轻松检索Films收藏,如下所示:

XDocument xdoc = XDocument.Load(xmldata);
List<Films> result = xdoc.Descendants("film")
              .Select(x =>
                        {
                           var film = x;
                           var title = x.Parent;
                           return new Film
                           {
                               month = (int)title.Attribute("month"),
                               year = (string)title.Attribute("year"),
                               day = (string)film.Attribute("day"),
                               morning = (string)film.Element("morning"),
                               night = (string)film.Element("night")
                           };
                        }
                   ).ToList();

This will return two films, and each will have month & year based on Title node. 这将返回两部电影,每部电影将基于“ Title节点具有月份和年份。

Code Explanation: 代码说明:

First we are finding all the film nodes, then projecting it using Select . 首先,我们找到所有film节点,然后使用Select对其进行投影。 In the select clause we can save variable for each film (you can think of film inside select method like alias in foreach loop). 在select子句中,我们可以为每部影片保存变量(您可以在select方法中想到film例如foreach循环中的别名)。 Also, we are storing the parent Title node in title variable. 另外,我们将父Title节点存储在title变量中。 After this all we need to do is read the elements & attributes. 在这之后,我们需要做的就是阅读元素和属性。

If understanding Method syntax is difficult, then here is the equivalent query syntax : 如果难以理解Method语法,则这里是等效的query syntax

List<Films> result2 = (from x in xdoc.Descendants("film")
                       let film = x
                       let title = x.Parent
                       select new Film
                       {
                           month = (int)title.Attribute("month"),
                           year = (string)title.Attribute("year"),
                           day = (string)film.Attribute("day"),
                           morning = (string)film.Element("morning"),
                           night = (string)film.Element("night")
                       }).ToList();

Working Fiddle 工作小提琴

You can parse your XML with the following: 您可以使用以下内容解析XML:

var xml = XDocument.Load(xmlFile);
var films = xml.Descendants("film").Select(d => new Films()
    {
        month = Convert.ToInt32(d.Parent.Attribute("month").Value),
        year = d.Parent.Attribute("year").Value,
        day = d.Attribute("day").Value,
        morning = d.Element("morning").Value,
        night = d.Element("night").Value
    });

See it in action HERE . 这里查看它的操作。

When i read your xml in a dataset i get 2 tables Table 1 "Title" with one row and 3 columns Table 2 "film" with two rows and 4 columns 当我在数据集中读取xml时,我得到2个表,表1“标题”有1行3列,表2“胶片”有2行4列

you read on Tables[0] (3 columns) - and you try to read 4th column of 3.. 您在Tables [0](3列)上阅读-并尝试读取3的第4列。

you need to change your loop - since you need to load Data from two tables. 您需要更改循环-因为您需要从两个表中加载数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM