简体   繁体   中英

How do I read Child Nodes in Xml file using MVC

## XML FİLE ##

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

where is the error? I want to read the xml file.

You can retrieve Films collection using Linq-To-XML easily like this:

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.

Code Explanation:

First we are finding all the film nodes, then projecting it using 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). Also, we are storing the parent Title node in title variable. 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 :

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:

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

you read on Tables[0] (3 columns) - and you try to read 4th column of 3..

you need to change your loop - since you need to load Data from two tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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