LINQ to XML

T

Tony Johansson

Hi!

Here I use LINQ to XML. What I want is to load the following elements
title, runningLength, productionYear,url and isbnNumber into a generic
collection of Movie object.
By code works but I find it far to long. It must be possible to make this
code much shorter.

List<Movie> movies = new List<Movie>();
var myMovies = from movie in
XDocument.Load(TextboxURL.Text).Descendants("movie")
select new
{
title = movie.Element("title").Value,
runningLength = movie.Element("runningLength").Value,
productionYear = movie.Element("productionYear").Value,
url = movie.Element("url").Value,
isbnNumber = movie.Element("isbnNumber").Value
};

foreach (var movie in myMovies )
{
movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32
(movie.runningLength),movie.productionYear,movie.isbnNumber));
}

//Tony
 
H

Harlan Messinger

Tony said:
Hi!

Here I use LINQ to XML. What I want is to load the following elements
title, runningLength, productionYear,url and isbnNumber into a generic
collection of Movie object.
By code works but I find it far to long. It must be possible to make this
code much shorter.

List<Movie> movies = new List<Movie>();
var myMovies = from movie in
XDocument.Load(TextboxURL.Text).Descendants("movie")
select new
{
title = movie.Element("title").Value,
runningLength = movie.Element("runningLength").Value,
productionYear = movie.Element("productionYear").Value,
url = movie.Element("url").Value,
isbnNumber = movie.Element("isbnNumber").Value
};

foreach (var movie in myMovies )
{
movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32
(movie.runningLength),movie.productionYear,movie.isbnNumber));
}

Untested, but:

List<Movie> movies = (
from movie in XDocument.Load(TextboxURL.Text).Descendants("movie")
select new Movie(
movie.Element("title").Value,
movie.Element("url").Value,
Convert.ToInt32(movie.Element("runningLength").Value),
movie.Element("productionYear").Value,
movie.Element("isbnNumber").Value
)
).ToList();
 
F

Family Tree Mike

Hi!

Here I use LINQ to XML. What I want is to load the following elements
title, runningLength, productionYear,url and isbnNumber into a generic
collection of Movie object.
By code works but I find it far to long. It must be possible to make this
code much shorter.

List<Movie> movies = new List<Movie>();
var myMovies = from movie in
XDocument.Load(TextboxURL.Text).Descendants("movie")
select new
{
title = movie.Element("title").Value,
runningLength = movie.Element("runningLength").Value,
productionYear = movie.Element("productionYear").Value,
url = movie.Element("url").Value,
isbnNumber = movie.Element("isbnNumber").Value
};

foreach (var movie in myMovies )
{
movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32
(movie.runningLength),movie.productionYear,movie.isbnNumber));
}

//Tony

Your code only is long because you are creating the anonymous type via
LINQ, then converting the results to the type you want. Depending on
your needs after creating the list, couldn't you simply do:

List<Movie> movies = from movie in
XDocument.Load(TextboxURL.Text).Descendants("movie")
select new Movie
{
title = movie.Element("title").Value,
runningLength = movie.Element("runningLength").Value,
productionYear = movie.Element("productionYear").Value,
url = movie.Element("url").Value,
isbnNumber = movie.Element("isbnNumber").Value
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top