简体   繁体   English

无法为此变量分配值

[英]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 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: 问题是UserWhoPosted是我制作的User.cs类POCO的对象:

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? 我尚未对此进行测试,但是在填充Comment对象时不能实例化Friend对象吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM