【问题标题】:List Value to XML conversion using LINQ使用 LINQ 将值列表转换为 XML
【发布时间】:2013-05-08 01:33:56
【问题描述】:

我在列表中有这种格式的数据

ItemA LoaderA ConfigA 30 ItemA LoaderA ConfigB 默认=180 ItemA LoaderA ConfigC 20 ItemB LoaderA ConfigA 30 ItemB LoaderA ConfigB 默认=120 ItemB LoaderA ConfigC 30 ItemC LoaderB ConfigD 30 ItemC LoaderB ConfigE 默认=120 ItemC LoaderB ConfigF 10 ItemA LoaderB ConfigD 30 ItemA LoaderB ConfigE 默认=30 ItemA LoaderB ConfigF 10

我正在尝试使用 LINQ 通过 ApplicationName 对数据进行分组(在本例中为 loaderA 和 B) 然后需要使用获得的数据按 ProductName(ItemA、ItemB 和 ItemC)分组,我需要生成一个 XML 文档 以下格式

<Application Name=LoaderA>
  <Product Name=ItemA>
    <Config Name=ConfigA>30</Config>
    <Config Name=ConfigB>Default=180</Config>
    <Config Name=ConfigC>20</Config>
  </Product>
  <Product Name=ItemB>
    <Config Name=ConfigA>30</Config>
    <Config Name=ConfigB>Default=120</Config>
    <Config Name=ConfigC>30</Config>
  </Product>
</Application>
<Application Name=LoaderB>
  <Product Name=ItemC>
    <Config Name=ConfigD>30</Config>
    <Config Name=ConfigE>Default=120</Config>
    <Config Name=ConfigF>20</Config>
  </Product>
  <Product Name=ItemA>
    <Config Name=ConfigD>30</Config>
    <Config Name=ConfigE>Default=120</Config>
    <Config Name=ConfigF>30</Config>
  </Product>
</Application>

有人可以分享我应该如何按项目分组,以便我利用 XElement 类以上述格式创建 XML。

【问题讨论】:

  • 列表的来源是什么?它在内存(列表列表)、文本文件等中吗?此外,您的 XML 无效 - 您需要一个不重复的根元素。
  • 数据来源来自文本文件。我很抱歉错过了根元素。
  • 这是文件的确切格式吗?它看起来像是用空格分隔的。
  • 其制表符分隔。 (4列由Tab分隔)
  • 您是想将文件转换为中间对象,然后使用该对象生成 XML,还是获取文件并直接从中生成 XML?

标签: c# xml linq


【解决方案1】:

正如您所说,您正在学习 LINQ,因此我使用 LINQ 的次数比平时多。 这会生成与您的完全相同的 xml,但属性被引号括起来。

class ListFileToXmlConverter
{
    private class Entry
    {
        public string Application { get; set; }
        public string Product { get; set; }
        public string Config { get; set; }
        public string Value { get; set; }
    }

    private IEnumerable<Entry> LoadEntries(string filename)
    {
        return File.ReadAllLines(filename)
            .Where(line => !String.IsNullOrWhiteSpace(line))
            .Select(line => line.Split(new[] {'\t'}))
            .Select(split => new Entry
                {
                    Product = split[0],
                    Application = split[1],
                    Config = split[2],
                    Value = split[3]
                });
    }

    public XElement ConvertToXml(string filename)
    {
        return new XElement("root",
            LoadEntries(filename)
                .GroupBy(entry => entry.Application)
                .Select(grouping =>
                    new XElement(
                        "Application",
                        new XAttribute("Name", grouping.Key),
                        grouping
                            .GroupBy(entry => entry.Product)
                            .Select(grouping2 =>
                                new XElement(
                                    "Product",
                                    new XAttribute("Name", grouping2.Key),
                                    grouping2.Select(entry =>
                                        new XElement("Config",
                                            new XAttribute("Name", entry.Config),
                                            entry.Value)
                                        )
                                    )
                            )
                        )
                )
            );
    }
}

【讨论】:

  • 非常感谢。非常感谢您的意见。
猜你喜欢
  • 2013-07-29
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多