跨两个类提取的XML数据

时间:2015-02-12 08:44:18

标签: c# xml linq

我有两个类和以下XML。我不知道如何使用以下XML创建Director和Movie的对象。我想使用Linq,但尝试过很多东西但不确定。

我希望所有导演对象都带有他们的ID,并且我希望所有的电影对象都包含所有字段。

public class Director
{
public int DirectorID;
public string Name;
}

public class Movie
{
public string DirectorID;
public string Genre;
public string Rating;
public string MovieName;
}



<?xml version="1.0" encoding="UTF-8"?>
<Movie xmlns="http://www.imdb.co.uk" CreationDate="2015-01-25T18:14:33" year="2012" NumberOfMovies="6">
   <DirectorMovie>
      <Director>
         <Name>Ridley Scott</Name>
      </Director>
      <Movie>
         <Genre>Horror</Genre>
         <Rating>8.9</Rating>
         <MovieName>Alien</MovieName>
      </Movie>
      <Movie>
         <Genre>Sci-Fi</Genre>
         <Rating>8.7</Rating>
         <MovieName>Blade Runner</MovieName>
      </Movie>
      <Movie>
         <Genre>Action</Genre>
         <Rating>7.4</Rating>
         <MovieName>Black Hawk Down</MovieName>
      </Movie>
   </DirectorMovie>
   <DirectorMovie>
      <Director>
         <Name>James Cameron</Name>
      </Director>
      <Movie>
         <Genre>Action</Genre>
         <Rating>8.9</Rating>
         <MovieName>Aliens</MovieName>
      </Movie>
      <Movie>
         <Genre>Sci-Fi</Genre>
         <Rating>8.5</Rating>
         <MovieName>Avatar</MovieName>
      </Movie>
      <Movie>
         <Genre>Action</Genre>
         <Rating>8.8</Rating>
         <MovieName>Prometheus</MovieName>
      </Movie>
   </DirectorMovie>
</Movie>

2 个答案:

答案 0 :(得分:0)

首先,要小心你如何定义你的类。目前,您已经创建了只具有公共属性的类,这可能不是您想要做的事情。检查如何创建propertiesauto-implemented properties

然后,就解析而言,使用Linq to XML非常简单(代码尚未经过测试,并且远未完成,可以用作入门):

var doc = XDocument.Parse(xmlData);

var movieElement = doc.Element("Movie")
                        .Elements("DirectorMovie")
                        .Elements("Movie")
                        .Select(x => new Movie{
                            DirectorId = GetDirectorIdFromName(x.Parent.Element("Director").Element("Name").Value),
                            Genre = x.Element("Genre").Value
                        });

答案 1 :(得分:0)

我认为,这会帮助你想要的东西 - 我认为id是自动生成的。

int iID = 1;
        XNamespace xn = "http://www.imdb.co.uk";
        // List of individual directors;
        var list = xeDir.Descendants(xn + "DirectorMovie").Select(item => new Director()
                                                    {
                                                        DirectorID = iID++,
                                                        Name = item.Element(xn + "Director").Element(xn + "Name").Value
                                                    });

        // Dictionary of directors name vs movies list.
        var movList = xeDir.Descendants(xn + "DirectorMovie").
            ToDictionary(dir => dir.Element(xn + "Director").Element(xn + "Name").Value,
                                          dir => dir.Elements(xn + "Movie").
                                              Select(mov => new Movie()
                                          {
                                              Genre = mov.Element(xn + "Genre").Value,
                                              MovieName = mov.Element(xn + "MovieName").Value,
                                              Rating = mov.Element(xn + "Rating").Value,
                                              DirectorID = list.Where(item => item.Name == dir.Element(xn + "Director").Element(xn + "Name").Value).First().DirectorID.ToString()

                                          })
                         );