如何从此XML中提取所有ID?

时间:2014-03-13 01:44:43

标签: c# xml

如何提取此xml文件中的所有元素,元素数量也可能不同

<root response="True">
<Movie Title="The Wolf of Wall Street" Year="2013" imdbID="tt0993846" Type="movie"/>
<Movie Title="Brotherhood of the Wolf" Year="2001" imdbID="tt0237534" Type="movie"/>
<Movie Title="Wolf Creek" Year="2005" imdbID="tt0416315" Type="movie"/>
<Movie Title="Teen Wolf" Year="2011–" imdbID="tt1998328" Type="series"/>
<Movie Title="Teen Wolf" Year="2011–" imdbID="tt1567432" Type="series"/>
<Movie Title="Wolf" Year="1994" imdbID="tt0111742" Type="movie"/>
<Movie Title="Teen Wolf" Year="1985" imdbID="tt0090142" Type="movie"/>
<Movie Title="The Wolf Man" Year="1941" imdbID="tt0034398" Type="movie"/>
<Movie Title="Hour of the Wolf" Year="1968" imdbID="tt0063759" Type="movie"/>
<Movie Title="Jin-Roh: The Wolf Brigade" Year="1999" imdbID="tt0193253" Type="movie"/>
</root>

我需要在c#

中提取电影的所有ID属性和im编程

1 个答案:

答案 0 :(得分:3)

XPath的另一种可能方法是使用LINQ-to-XML和XDocument

var doc = XDocument.Load("path_to_xml_file.xml");
List<string> Ids = doc.Root
                      .Elements("Movie")
                      .Select(o => (string)o.Attribute("imdbID"))
                      .ToList();