0

我已经从这样的 csv 文件创建了一个 xml 文件

private void button2_Click(object sender, EventArgs e)
        {
            String[] FileContent = File.ReadAllLines(csvPathFile);
            String XMLNS = "";
            int idCountProduct = 1;
            XElement Inv = new XElement("data",
                from items in FileContent
                let fields = items.Split(';')
                select new XElement("category",
                    new XAttribute("idCategory", fields[0]),
                    new XAttribute("CategoryName", fields[1]),
                    new XElement("products",
                        new XElement("product",
                            new XAttribute("IdProduct", idCountProduct++),
                            new XAttribute("Rif", fields[0]),
                            new XAttribute("ProductName", fields[2]),

                            new XElement("products",
                                new XElement("product",
                                    new XAttribute("IdProduct", idCountProduct++),
                                    new XAttribute("Rif", fields[0]),
                                    new XAttribute("ProductName", fields[3]),
                                    new XElement("products",
                new XElement("product",
                    new XAttribute("IdProduct", idCountProduct++),
                    new XAttribute("Rif", fields[0]),
                    new XAttribute("ProductName", fields[4]));
            File.WriteAllText(xmlPathFile, XMLNS + Inv.ToString());
        }

这是我的 csv 文件

1;Beverages;Lemon Juice;;Orange Juice

这是我要创建的 xml 文件

<data>
<category idCategory="1" CategoryName= "Beverages">
    <products>
      <product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
      <product IdProduct="2" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
</data>

这是我获得的xml文件

<data>
<categories>
<category idCategory="1" CategoryName= "Beverages">
    <products>
      <product IdProduct="1" Rif="1" ProductName= "Lemon Juice" />
      <product IdProduct="2" Rif="1" ProductName= "" />
      <product IdProduct="3" Rif="1" ProductName= "Orange Juice" />
<products/>
<category/>
<categories/>
</data>

如果未分配 ProductName,如何避免添加产品?

4

1 回答 1

0

向您的 LINQ 查询添加过滤器以过滤掉空产品:

where !String.IsNullOrEmpty(fields[4])

就在这之后:

let fields = items.Split(';')

无论如何,如果您的条件如此简单,您甚至不需要它,您可以从 Split 指令本身中过滤掉条目:

let fields = items.Split(';', StringSplitOptions.RemoveEmptyEntries)

编辑 您的代码非常固定,所以...您确定需要使用 LINQ 和 LINQ-to-XML 吗?我想它无论如何都会更具可读性......写这个:

        XElement data = new XElement("data");
        XDocument document = new XDocument(data);

        int counter = 0;
        foreach (string entry in File.ReadAllLines(csvPath))
        {
            string[] fields = entry.Split(new char[] { ';' },
                                StringSplitOptions.RemoveEmptyEntries);

            XElement category = new XElement("category",
                new XAttribute("idCategory", fields[0]),
                new XAttribute("CategoryName", fields[1]));
            data.Add(category);

            XElement products = new XElement("products");
            category.Add(products);

            for (int i = 2; i < fields.Length; ++i)
            {
                products.Add(new XElement("product",
                    new XAttribute("IdProduct", counter++),
                    new XAttribute("Rif", fields[0]),
                    new XAttribute("ProductName", fields[i])));
            }
        }

        document.Save(xmlPath);

}

于 2012-04-19T10:19:16.353 回答