2012-03-23 36 views
0

我做一个服务,在XML返回数据的集成。 我反序列化他们到使用XML序列化的数据列表:环路上的属性值泛型列表<T>

[Serializable, XmlRoot("string", Namespace = "http://tempuri.org/", IsNullable = true)] 
public class PositionPosting 
{ 
    [XmlArray("JobCategories")] 
    [XmlArrayItem("JobCategory", typeof(ExportItems))] 
    public ExportItems[] JobCategory { get; set; } 
} 


[Serializable()] 
public class ExportItems 
{ 
    [XmlAttribute("key")] 
    public string Key { get; set; } 

    [XmlAttribute("text")] 
    public string Value { get; set; } 
} 

至于结果,我有这样的:enter image description here

我提供GetServiceData方法有什么需要序列化到XML模型。

在此之后我创建将得到这个列表就像IList的方法:

private void UpdateDropDown<T>(IList<DropDownModel> model, IList<T> syncData) where T : class 

?我怎样才能从JobCategories阵列中的IList syncData属性和名称?

感谢您的帮助!

+0

参数'syncData'只是与class'的'唯一约束T'的'泛型集合,所以从理论上讲,所有你需要做的就是将结果转换到'名单'对象,并通过他们在这样肯定? 编辑:如果结构是一个数组,使用'System.Linq'将允许你调用'ToList()'。 – Richard 2012-03-23 10:57:09

+0

你可以写一个例子,因为我不知道我理解你... – 2012-03-23 11:14:42

回答

0

这个问题有点混乱;所以这里是我通过几次阅读可以得到的假设。

  1. GetServiceData(..)正在恢复的ExportItems
  2. 你想一个数组的信息传递给UpdateDropDown<T>(..)

现在,转换的ExportItems[]IList<ExportItem>,你只需要创建一个对象的实例实施IList<T>,最好的例子是List<T>

var data = this.GetServiceData<PositionPosting>(this.GetService(id)); 
UpdateDropDown(model, data[0].ToList()) 

希望这有助于:这可以通过首先包含命名空间System.Linq使用LINQ的内置的扩展,像这样做,然后在“ToList'ing”的阵列。 :)

问: 什么都在this.GetServiceData<T>(..)的约束?