列出使用LINQ进行XML转换的值

时间:2013-05-08 01:33:57

标签: c# xml linq

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

ItemA   LoaderA ConfigA 30
ItemA   LoaderA ConfigB Default=180
ItemA   LoaderA ConfigC 20

ItemB   LoaderA ConfigA 30
ItemB   LoaderA ConfigB Default=120
ItemB   LoaderA ConfigC 30

ItemC   LoaderB ConfigD 30
ItemC   LoaderB ConfigE Default=120
ItemC   LoaderB ConfigF 10

ItemA   LoaderB ConfigD 30
ItemA   LoaderB ConfigE Default=30
ItemA   LoaderB ConfigF 10

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

<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。

1 个答案:

答案 0 :(得分:1)

正如你所说,你正在学习LINQ,所以我使用的LINQ比平时更多。 这会产生与您的xml完全相同的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)
                                        )
                                    )
                            )
                        )
                )
            );
    }
}