嵌套的LiNQ to XML [英] Nested LiNQ to XML

查看:77
本文介绍了嵌套的LiNQ to XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种保存/加载以下内容的好方法. 我想另存为XML,理想情况下希望使用LiNQ(即是为了帮助我学习LINQ)

I am looking for a nice way to save / load the following. I want to save as XML and ideally looking to use LiNQ (namely to help me learn LINQ)

我不知道如何做嵌套的linq.有人可以帮忙吗?

I don't know how to do nested linq writes though. Can anyone help?

    /// <summary>
/// 
/// </summary>
public class ErrorType
{
    List<ErrorType> _childErrors;

    public String Name { get; set; }
    public bool Ignore { get; set; }

    public List<ErrorType> ChildErrors { get; protected set; }
}

/// <summary>
/// 
/// </summary>
public class ErrorList
{
    public List<ErrorType> ChildErrors { get; protected set; }

    public void Save()
    {
    }

    public void Load()
    {
    }
}

基本上,ErrorList包含一个错误的顶级列表,每个错误可以有子级. XML输出应类似于:

Essentially the ErrorList contains a top level list of Errors, each error can have children. The XML output should look something like:

<ErrorList>
<ErrorName1 Ignore="false">
<ChildErrorName1 Ignore="true">
<ChildErrorName2 Ignore="false" />
</ChildErrorName1>
</ErrorName1>
<ErrorList>

如果有人可以帮助,那就太好了. 谢谢

If anyone could help that would be great. Thanks

推荐答案

好的,我想我明白你现在要干什么.试试这个:

Okay, I think I see what you're after now. Try this:

// Need to declare in advance to call within the lambda.
Func<ErrorType, XElement> recursiveGenerator = null;
recursiveGenerator = error => new XElement
    (error.Name,
     new XAttribute("Ignore", error.Ignore),
     error.ChildErrors.Select(recursiveGenerator));

var errorList = new XElement
    ("ErrorList", errors.ChildErrors.Select(recursiveGenerator));

这是一个完整的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

public class ErrorType
{
    public String Name { get; set; }
    public bool Ignore { get; set; }

    public List<ErrorType> ChildErrors { get; protected set; }
    public ErrorType()
    {
        ChildErrors = new List<ErrorType>();
    }
}

public class ErrorList
{
    public List<ErrorType> ChildErrors { get; protected set; }
    public ErrorList()
    {
        ChildErrors = new List<ErrorType>();
    }
}

public class Test
{
    public static void Main()
    {
        var childError2 = new ErrorType { 
            Name = "ChildErrorName2", Ignore=false };
        var childError1 = new ErrorType {
            Name = "ChildErrorName1", Ignore=true,
            ChildErrors = { childError2 }
        };
        var mainError = new ErrorType {
            Name = "ErrorName1", Ignore=true,
            ChildErrors = { childError1 }
        };
        var errorList = new ErrorList { ChildErrors = { mainError } };

        // Need to declare in advance to call within the lambda.
        Func<ErrorType, XElement> recursiveGenerator = null;
        recursiveGenerator = error => new XElement
            (error.Name,
             new XAttribute("Ignore", error.Ignore),
             error.ChildErrors.Select(recursiveGenerator));

        var element = new XElement
             ("ErrorList", 
              errorList.ChildErrors.Select(recursiveGenerator);

        Console.WriteLine(element);
    }        
}

这篇关于嵌套的LiNQ to XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆