首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果XAttribute = "“,csv to xml不添加xElement

如果XAttribute = "“,csv to xml不添加xElement
EN

Stack Overflow用户
提问于 2012-04-19 18:10:42
回答 1查看 348关注 0票数 0

我已经从csv文件创建了一个xml文件,如下所示

代码语言:javascript
复制
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文件

代码语言:javascript
复制
1;Beverages;Lemon Juice;;Orange Juice

这是我要创建的xml文件

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
<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,如何避免添加产品?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-19 18:19:16

向您的LINQ查询添加筛选器,以筛选出空产品:

代码语言:javascript
复制
where !String.IsNullOrEmpty(fields[4])

紧接着:

代码语言:javascript
复制
let fields = items.Split(';')

无论如何,如果你的条件很简单,你甚至不需要它,你可以过滤掉拆分指令本身的条目:

代码语言:javascript
复制
let fields = items.Split(';', StringSplitOptions.RemoveEmptyEntries)

编辑你的代码是非常固定的,你确定你需要使用LINQ so...are - to -XML吗?我猜它会更具可读性,anyway...write如下:

代码语言:javascript
复制
        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);

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10225915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档