簡體   English   中英

無法為此變量分配值

[英]Trouble assigning a value to this variable

使用此代碼,我打算填充變量。

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

問題是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; }
    }
}

我將如何填充該對象?

我尚未對此進行測試,但是在填充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