BLACKMAMBA
BLACKMAMBA

Reputation: 725

How to Extract all IDs from this XML?

How can i extract all the elements inside this xml file,the number of elements can vary also

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

i need to extract all the IDs attribute of movie and im programming in c#

Upvotes: 1

Views: 231

Answers (1)

har07
har07

Reputation: 89315

One possible way beside XPath is, using LINQ-to-XML and XDocument :

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

Upvotes: 3

Related Questions