我有一个博客导出包,可以将Umbraco中的博客内容导出为XML。
现在我要导出注释数据,注释部分在NewsItem节点上设置为childNode,我如何使用这种格式从childNode中获取数据到列表中?
这是我的代码:
public List<BlogPosts> getPostList()
{
var contentType = ApplicationContext.Current.Services.ContentTypeService
.GetContentType("umbNewsItem");
var nodes = ApplicationContext.Current.Services.ContentService
.GetContentOfContentType(contentType.Id).Select(content => new Node(content.Id));
return nodes.Select(node => new BlogPosts()
{
Title = node.GetProperty("title").ToNullSafeString(),
BodyText = node.GetProperty("bodyText").ToNullSafeString(),
PublishDate = node.GetProperty("publishDate").ToNullSafeString(),
Author = node.GetProperty("author").ToNullSafeString(),
Image = node.GetProperty("image").ToNullSafeString(),
//This is where I want to grab the blog comments content
Comments = node.ChildrenAsList.Add("comments")
}).ToList();
}
我第一次尝试这个,我在.Add(“comments”)行上看到一个错误:
The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments
我接下来要做的就是:
Comments = node.ChildrenAsList<BlogComment>.Add("comments").ToList()
返回以下错误:
The property 'umbraco.NodeFactory.Node.ChildrenAsList' cannot be used with type arguments
我也试过这个:
Comments = node.ChildrenAsList.Add("comments").ToList()
返回了此错误:
The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments
这是我的BlogPosts模型:
public class BlogPosts
{
public string Title { get; set; }
public string BodyText { get; set; }
public string PublishDate { get; set; }
public string Author { get; set; }
public string Image { get; set; }
public List<BlogComment> Comments { get; set; }
}
public class BlogComment
{
public string Comment { get; set; }
public string CommentDate { get; set; }
}
这是Umbraco后台页面的一个示例: Image
我在stackoverflow和google中搜索了任何涉及从childNode调用数据到列表的内容,但是这里的列表类型是INode,当使用它时:
Comments = node.ChildrenAsList
它会返回此错误:
Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'
答案 0 :(得分:2)
那么: - )
首先,.Add()尝试向集合添加内容,以便这样做 不会在这里工作。
其次,我认为选择内容作为节点有点倒退,所以我 我会尽量不这样做。
第三,IEnumerable有一个我认为可能有效的Cast()方法 这里。不过,我无法真正测试它。
同样,这是非常未经测试的,但也许尝试这样的事情?显然我不知道评论DocType别名,所以记得改变那一点: - )
public List<BlogPosts> getPostList()
{
var contentType = UmbracoContext.Current.Application.Services.ContentTypeService
.GetContentType("umbNewsItem");
var contentService = UmbracoContext.Current.Application.Services.ContentService;
var nodes = contentService.GetContentOfContentType(contentType.Id);
return nodes.Select(node => new BlogPosts()
{
Title = node.GetValue("title").ToNullSafeString(),
BodyText = node.GetValue("bodyText").ToNullSafeString(),
PublishDate = node.GetValue("publishDate").ToNullSafeString(),
Author = node.GetValue("author").ToNullSafeString(),
Image = node.GetValue("image").ToNullSafeString(),
//This is where I want to grab the blog comments content
Comments = contentService.GetChildren(node.Id).Where(x => x.ContentType.Alias == "Comment").Cast<BlogComment>().ToList()
}).ToList();
}