【发布时间】:2016-07-14 16:01:44
【问题描述】:
背景
我目前正在尝试向模型列表中添加一个值,但这似乎与我在 JavaScript 中的操作不同。
型号
public class ContentBase
{
public string Name { get; set; }
public string Description { get; set; }
public string LastModifiedTime { get; set; }
public string KeyWords { get; set; }
public string Content { get; set; }
public string UniqueId { get; set; }
public string library { get; set; }
public string ContentSummary { get; set; }
public string[] images { get; set; }
}
增值功能
List<ContentBase> returnList = new List<ContentBase>();
foreach (HtmlNode img in htmlDocument.DocumentNode.SelectNodes("//img"))
{
HtmlAttribute att = img.Attributes["src"];
returnList.Add(images = att.Value); << this is wrong, just trying to give an idea of my intentions
}
问题
我正在尝试将来自HtmlAttribute att = img.Attributes["src"]; 的结果添加到我列表的string[] images 中。这样,一旦我完成了这个迭代,我将能够在我的列表的 string[] images 部分中拥有我从 for 每个循环中获得的所有值
更新
这是填充列表中其他项目的前面的函数
string content = "";
if (includeContent == true)
{
content = rslt[ContentBase.fastContent] != null ? rslt[ContentBase.fastContent].ToString() : "";
}
returnList.Add(new ContentBase()
{
UniqueId = rslt[ContentBase.fasstUniqueId] != null ? rslt[ContentBase.fasstUniqueId].ToString() : "",
LastModifiedTime = rslt[ContentBase.fasstLastModifiedTime] != null ? rslt[ContentBase.fasstLastModifiedTime].ToString().Substring(0, 8) : "",
Name = rslt[ContentBase.fastName] != null ? rslt[ContentBase.fastName].ToString() : "",
Description = rslt[ContentBase.fastDescription] != null ? rslt[ContentBase.fastDescription].ToString() : "",
KeyWords = rslt[ContentBase.fastKeyWords] != null ? rslt[ContentBase.fastKeyWords].ToString() : "",
ContentSummary = rslt[ContentBase.fasstContentSummary] != null ? rslt[ContentBase.fasstContentSummary].ToString() : "",
Content = content,
});
【问题讨论】:
-
试图将 att.Value 添加到返回列表内的 Images 属性中
-
列表是否只包含一个您设置其
images属性的ContentBase元素? -
离开了,是的,它将包含从我的每个循环接收到的元素。元素计数将是动态的而不是静态的
-
for循环只能提供属于单个
ContentBase的images。其他ContentBase对象来自哪里? -
尽管我可能没有答案,但我认为一次推送所有项目可能是一个好主意,如果不是,您将获得仅包含图像 Url 的列表项目
标签: c# .net list generic-list