开发者

Trouble assigning a value to this variable

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,
               开发者_如何学C     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?


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜