2010-10-25 2 views
1

En utilisant ce code, j'ai l'intention de remplir les variables.Problème lors de l'attribution d'une valeur à cette 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(); 

Le problème est UserWhoPosted est un objet de la classe User.cs POCO j'ai fait:

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 pourrais-je remplir cet objet?

Répondre

0

Je n'ai pas testé cela mais ne pouvez-vous instancier l'objet ami lorsque vous remplissez l'objet Comment?

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

Oui, cela fonctionne exactement comme je le voulais. –

Questions connexes