2015-02-12 53 views
0

我有两个类和下面的XML。我不知道如何从以下XML中创建Director和Movie的对象。我想使用Linq,但尝试了很多东西,但并不确定。跨两个类提取的XML数据

我想拥有所有导演对象及其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> 
+1

<默认注释>你尝试过什么到目前为止? oopbase 2015-02-12 08:53:06

+0

哪里是'DirectorID'在你的XML? – 2015-02-12 09:11:06

回答

1

我想,这将帮助你与你想要的 - 我假设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() 

              }) 
         ); 
1

首先,要小心如何定义你的班级。目前,您已经创建了仅具有公共属性的类,这可能不是您想要执行的操作。检查如何创建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 
         });