使用此代码我打算填充变量。
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; }
}
}
我如何填充该对象?
答案 0 :(得分:0)
我没有测试过这个,但是当你填充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();