【问题标题】:Nested LiNQ to XML将 LiNQ 嵌套到 XML
【发布时间】:2009-06-02 09:38:06
【问题描述】:

我正在寻找一种保存/加载以下内容的好方法。 我想保存为 XML 并希望使用 LiNQ(即帮助我学习 LINQ)

我不知道如何进行嵌套 linq 写入。有人可以帮忙吗?

    /// <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 输出应类似于:

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

如果有人可以提供帮助,那就太好了。 谢谢

【问题讨论】:

    标签: c# .net xml linq


    【解决方案1】:

    好吧,我想我明白你现在在追求什么了。试试这个:

    // 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);
        }        
    }
    

    【讨论】:

    • 干杯乔恩,看起来不错。我对其进行了测试,但它在行上有一个编译器错误:errorList.ChildErrors.Select(recursiveGenerator(x));我用 errorList.ChildErrors[0] 替换了 X 以匹配正确的类型,但没有乐趣。我的 lambda 技能很弱,所以无法弄清楚出了什么问题。
    • 哎呀,修复了 - 它应该是 errorList.ChildErrors.Select(recursiveGenerator);
    • (我曾经有errorList.ChildErrors.Select(x => recursiveGenerator(x)),有点多余。)
    • 谢谢,看起来不错,我将如何反转它以实际从 XElement 加载数据?
    • 到目前为止我有这个... private ErrorList FromXmlToErrorList(XElement xd) { List typeList = (from error in xd.Descendants() select new ErrorType { Name = error.Value, Ignore = bool.Parse(error.Attribute("Ignore").Value) ChildErrors= /*Lambda ?*/ }).ToList();错误列表 el = new ErrorList(); el.ChildErrors = 类型列表;返回 el; }
    【解决方案2】:

    看看this。它应该是你正在寻找的东西。没有 LINQ 的想法,而是输出 XML 的好方法。

    不过,底线是使用XmlSerializer

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多