Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Umbraco子节点设置为列表<;T>;_C#_Xml_Umbraco7_Export To Xml - Fatal编程技术网

C# 如何将Umbraco子节点设置为列表<;T>;

C# 如何将Umbraco子节点设置为列表<;T>;,c#,xml,umbraco7,export-to-xml,C#,Xml,Umbraco7,Export To Xml,我有一个博客导出包,它可以将Umbraco中的博客内容导出为XML 现在我想导出注释数据,注释部分设置为NewsItem节点上的childNode,如何使用此格式将数据从childNode抓取到列表中 这是我的密码: public List<BlogPosts> getPostList() { var contentType = ApplicationContext.Current.Services.ContentTypeService .GetContent

我有一个博客导出包,它可以将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();
}
我也试过:

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
Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'
它返回以下错误:

The best overloaded method match for 'System.Collections.Generic.List<umbraco.interfaces.INode>.Add(umbraco.interfaces.INode)' has some invalid arguments
Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
那好吧:-)

  • 首先,.Add()尝试向集合中添加一些内容,以便 在这里不行

  • 其次,我认为选择内容作为节点有点倒退,所以我 我会尽量不那样做

  • 第三,IEnumerable有一个Cast()方法,我认为它可能有效 在这里不过,我不能真正测试它

再说一次,这是非常未经测试的,但也许可以尝试这样的东西?显然我不知道注释DocType别名,所以请记住更改该位:-)

public List getPostList()
{
var contentType=UmbracoContext.Current.Application.Services.ContentTypeService
.GetContentType(“umbNewsItem”);
var contentService=UmbracoContext.Current.Application.Services.contentService;
var节点=contentService.GetContentOfContentType(contentType.Id);
返回nodes.Select(node=>newblogposts()
{
Title=node.GetValue(“Title”).ToNullSafeString(),
BodyText=node.GetValue(“BodyText”).ToNullSafeString(),
PublishDate=node.GetValue(“PublishDate”).ToNullSafeString(),
Author=node.GetValue(“Author”).ToNullSafeString(),
Image=node.GetValue(“Image”).tonullsafetString(),
//这就是我想要抓取博客评论内容的地方
Comments=contentService.GetChildren(node.Id).Where(x=>x.ContentType.Alias==“Comment”).Cast().ToList()
}).ToList();
}
那好吧:-)

  • 首先,.Add()尝试向集合中添加一些内容,以便 在这里不行

  • 其次,我认为选择内容作为节点有点倒退,所以我 我会尽量不那样做

  • 第三,IEnumerable有一个Cast()方法,我认为它可能有效 在这里不过,我不能真正测试它

再说一次,这是非常未经测试的,但也许可以尝试这样的东西?显然我不知道注释DocType别名,所以请记住更改该位:-)

public List getPostList()
{
var contentType=UmbracoContext.Current.Application.Services.ContentTypeService
.GetContentType(“umbNewsItem”);
var contentService=UmbracoContext.Current.Application.Services.contentService;
var节点=contentService.GetContentOfContentType(contentType.Id);
返回nodes.Select(node=>newblogposts()
{
Title=node.GetValue(“Title”).ToNullSafeString(),
BodyText=node.GetValue(“BodyText”).ToNullSafeString(),
PublishDate=node.GetValue(“PublishDate”).ToNullSafeString(),
Author=node.GetValue(“Author”).ToNullSafeString(),
Image=node.GetValue(“Image”).tonullsafetString(),
//这就是我想要抓取博客评论内容的地方
Comments=contentService.GetChildren(node.Id).Where(x=>x.ContentType.Alias==“Comment”).Cast().ToList()
}).ToList();
}

Dude。你必须描述你尝试过的东西。在询问堆栈溢出之前,您必须表明您只做了一点研究。你必须描述什么不起作用。否则我们不能(或不会)帮助你。我看着这个问题,想“他有什么问题?”因为你没有包括任何错误信息或任何你已经尝试过的信息。。。这里有一个很好的问题:Stack Overflow并不是一个“我想要这个,请为我做”的论坛。你必须(想)去做,并从中学习。然后,当你真的陷入困境时,你已经研究了所有东西,并且尝试了所有你能想到的东西,你可以把所有这些都放到你的问题中,并得到更好的帮助。我甚至尝试过:
Comments=node.ChildrenAsList.Find(System.PredicateDude。您必须描述您尝试过的内容。在询问堆栈溢出问题之前,您必须表明您只做了一点研究。您必须描述哪些内容不起作用。否则,我们无法(或不会)帮助您。我看着这个问题,想“他有什么问题?”因为你没有包含任何错误信息或任何关于你已经尝试过的内容的信息…这里有一个好问题的帮助:堆栈溢出不是一个“我想要这个,请为我做它”的论坛。你必须(想要)这样做并从中学习。然后,当你真的陷入困境时,你已经研究了一切,并且尝试了你能想到的一切,你可以把所有这些都放在你的问题中,并得到更好的帮助。我甚至尝试过:
Comments=node.ChildrenAsList.Find(System.PredicateThanks,不幸的是
Comments=contentService.GetChildren(node.Id).Where(x=>x.ContentType.Alias==“Comment”).Cast().ToList()
未编译,我得到一个错误,看起来如下:
非静态字段、方法或属性“Umbraco.Core.Services.ContentService.GetChildren(int)”需要对象引用“
感谢您的输入,这确实帮助我更深入地了解了我在做什么。文档类型的别名是“comments”它为我编译,对我来说很好?你是不是碰巧混合了静态和非静态方法/类?你想让我给你发送一个pastebin链接来显示整个类吗?我的类中没有任何静态方法,尽管这里是pastebin:啊,我在return语句上方创建了一个contentService对象(因为它被使用了不止一次)。也复制VAR。不幸的是,它不起作用,但是我正在寻找其他方法来实现我想要的。虽然这不是我目前最重要的项目。谢谢,不幸的是,
评论=
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; }
}
Comments = node.ChildrenAsList
Cannot implicitly convert type 'System.Collections.Generic.List<umbraco.interfaces.INode>' to 'System.Collections.Generic.List<UmbracoBlogsExportPackage.Models.BlogComment>'
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();
}