[英]csv to xml not adding xElement if XAttribute = “”
I've create an xml file from a csv file like this 我已经从这样的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());
}
this is my csv file 这是我的csv文件
1;Beverages;Lemon Juice;;Orange Juice
this is the xml file i want to create 这是我要创建的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>
and this is the xml file i obtain 这是我获得的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>
How can I avoid adding a product if ProductName is not assigned? 如果未分配ProductName,如何避免添加产品?
Add a filter to your LINQ query to filter out empty products: 将过滤器添加到LINQ查询中以过滤出空产品:
where !String.IsNullOrEmpty(fields[4])
just after this: 在此之后:
let fields = items.Split(';')
Anyway if your condition is so simple you do not even need it and you may filter out entries from the Split instruction itself: 无论如何,如果您的条件如此简单,您甚至不需要它,并且可以从Split指令本身中过滤出条目:
let fields = items.Split(';', StringSplitOptions.RemoveEmptyEntries)
EDIT Your code is pretty fixed so...are you sure you need to use LINQ and LINQ-to-XML? 编辑您的代码是相当固定的,因此...确定要使用LINQ和LINQ-to-XML吗? I guess it'll be even more readable anyway...write this:
我想无论如何它都会更具可读性...编写如下:
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);
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.