Hello,
I would like to be able to Read from a CSV file that will have "fields", lets call them ID, Name, Price. I want to be able to create a seperate XML file for each row in the CSV file. Right now, i have it so it will read all the lines in the csv file and make one big xml. The CSV file contains this:
1,Apple,23,44
2, Banana,33,45
3,Pie,111,66
The xml file it creates looks like this:
"
<Invoice>
<Store>
<Item>
<ID>1</ID>
<Name>Apple</Name>
<Price>23</Price>
</Item>
<Item>
<ID>2</ID>
<Name> Banana</Name>
<Price>33</Price>
</Item>
<Item>
<ID>3</ID>
<Name>Pie</Name>
<Price>111</Price>
</Item>
</Store>
</Invoice>
"
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
namespace CsvToXml
{
class Program
{
static void Main(string[] args)
{
String[] FileContent = File.ReadAllLines(@"C:\Users\Zach\Desktop\test.csv");
String XMLNS = "";
XElement Inv = new XElement("Invoice",
new XElement("Store",
from items in FileContent
let fields = items.Split(',')
select new XElement("Item",
new XElement("ID", fields[0]),
new XElement("Name", fields[1]),
new XElement("Price", fields[2])
)
)
);
File.WriteAllText(@"C:\Users\Zach\Desktop\text.xml", XMLNS + Inv.ToString());
}
}
}
Any help would be appreciated!! thanks!