4

How to fill Object Array inside a LINQ Query

Here is my data model

public class Test
    {
        public string Link { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public Item[] Items { get; set; }
    }

 public class Item
    {
        public string Title { get; set; }
        public string Link { get; set; }
        public string Guid { get; set; }
        public DateTime PublishDate { get; set; }
        public string Description { get; set; }
    }

And here is the Query

var data = from feed in feedXml.Descendants("channel")
                        select new Rss
                        {
                            Title = feed.Element("title").Value,
                            Link = feed.Element("link").Value,
                            Description = feed.Element("description").Value,
                            Items = // here i have to fill the array of items
                        };

UPDATE xml formate

<channel>
  .....  
  <item>.....</item>
  <item>.....</item>
  <item>.....</item>
</channel>
<channel>
  .....  
  <item>.....</item>
  <item>.....</item>
  <item>.....</item>
</channel>
<channel>
  .....  
  <item>.....</item>
  <item>.....</item>
  <item>.....</item>
</channel>
1
  • provide your xml file format. Commented Feb 7, 2014 at 0:03

1 Answer 1

6

You just need to make another query inside of your query. For example if channel element contains item elements then you can do:

Items = feed.Elements("item")
           .Select(x => new Item { // set property values })
           .ToArray()

Update: It seems you are reading a RSS file, so your query should be something like this:

var data = from feed in feedXml.Descendants("channel")
            select new Rss
            {
                Title = (string) feed.Element("title"),
                Link = (string) feed.Element("link"),
                Description = (string) feed.Element("description"),
                Items = feed.Elements("item")
                    .Select(
                        x =>
                            new Item
                            {
                                Title = (string) x.Element("title"),
                                Link = (string) x.Element("link"),
                                Description = (string) x.Element("description"),
                                Guid = (string) x.Element("guid"),
                                PublishDate = (DateTime) x.Element("pubDate")
                            })
                    .ToArray()

            };

Also I have used explicit cast instead of trying to access Value property directly to prevent NullReferenceException.

Sign up to request clarification or add additional context in comments.

1 Comment

@AhmadAbbasi actually I mean the item element's format.But that's ok see my update, if the element names are wrong you can change it.At least you must have an idea now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.