1 Attachment(s)
Retrieving a list from a XML to LINQ query
vb.net Code:
public class FeedDefinition
{
public string Name { get; set; }
public string Url { get; set; }
}
public List<FeedDefinition> LoadFeeds(string path)
{
XDocument feedXML = XDocument.Load(path);
var feeds = from feed in feedXML.Descendants("Feed")
select new FeedDefinition
{
Name = feed.Element("Name").Value,
Url = feed.Element("Url").Value
};
return feeds.ToList();
}
This code returns a list with each element queried from the feed, how does it "work"? In other words, first the names and then the Urls? How?
Thanks in advance.
I've attached a picture of the xml file model.
It was obtained from here.
Re: Retrieving a list from a XML to LINQ query
hey,
i didn't understand what you actually need
Re: Retrieving a list from a XML to LINQ query
In the 18th line of the code I posted, it says:
Code:
return feeds.ToList();
How is the content organized in this list? I mean, seeing that the class FeedDefinition has two strings: Name and Url; is the information recovered from the query stored in one single list or does it create two individual lists for each public string (Name and Url).
In other words, what does ToList() mean? I know it creates a generic list, but is it for each item or it contains the information queried from each string?
Re: Retrieving a list from a XML to LINQ query
hey,
actually your list comes from feeds and this is a list of items
but your item divided into Name and Url
as i can see your item here is FeedDefinition
and FeedDefinition is an object contains of 2 strings, so you have a got a list of FeedDefinitions :)
Re: Retrieving a list from a XML to LINQ query
Thanks for that. The reason I asked how each item in the list is stored it is because I want to know how to be able to get a specific item from that list, i.e Names. How would I go to do that?