tags:

views:

43

answers:

1

Using this code I intend to populate the variables.

http://www.dreamincode.net/forums/xml.php?showuser=146038

//Load comments.
var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments = (from comment in commentsXML.Descendants("comment")
                select new Comment()
                {
                    ID = comment.Element("id").Value,
                    Text = comment.Element("text").Value,
                    Date = comment.Element("date").Value,
                    UserWhoPosted = comment.Element("user").Value
                }).ToList();

The problem is UserWhoPosted is an object of the User.cs class POCO I made:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpDIC.Entities
{
    public class Friend
    {     
        public string ID { get; set; }
        public string Name { get; set; }
        public string Url { get; set; }
        public string Photo { get; set; }
    }
}

How would I populate that object?

+1  A: 

I haven't tested this but can't you instantiate the Friend object when your populating the Comment object?

var commentsXML = xml.Element("ipb").Element("profile").Element("comments");
this.Comments = 
(
    from comment in commentsXML.Descendants("comment")
    select new Comment()
    {
        ID = comment.Element("id").Value,
        Text = comment.Element("text").Value,
        Date = comment.Element("date").Value,
        UserWhoPosted = new Friend()
        {
            ID = comment.Element("user").Descendants("id"),
            Name = comment.Element("user").Descendants("name"),
            Url = comment.Element("user").Descendants("url"), 
            Photo =  comment.Element("user").Descendants("photo")
        }
    }
).ToList();
timothyclifford
Yeah, this works exactly how I wanted it to.
Serg