I am new to C# and all ive been coding have been filewatchers that would trigger and event when a certain file appears in the folder but recently i have been given a task to generate an XML file from a CSV file.
Ive done some googling and i did find some code that uses File.ReadAllLines
to read in the csv file and then use XElement
to create the XML file
......................
string[] source = File.ReadAllLines("DailyEZETaranTrades.csv");
source = source.Skip(2).Take(source.Length - 1).ToArray();
XElement cust = new XElement("GenevaLoader",
from str in source.Skip(2).Take(source.Length - 1)
let fields = str.Split(',')
select new XElement("Buy_New",
new XElement("RecordID", fields[0].Replace(" ", "")),
new XElement("Portfolio", fields[1].Replace(" ", "")),
new XElement("Broker", fields[2].Replace(" ", "")),
new XElement("Custodian", fields[3].Replace(" ", "")),
new XElement("Transaction", fields[4].Replace(" ", "")),
new XElement("Status", fields[5].Replace(" ", "")),
new XElement("SecurityType", fields[6].Replace(" ", "")),
new XElement("Security", fields[7].Replace(" ", "")),
new XElement("SecurityDescription", fields[8].Replace(" ", "")),
....
....
....
this works great except my cvs file will have different categories ... For example some lines will be BUYs and some will be SELLs. How can i parse the CSV to the column that contains the BUY or SELL text and then use a conditional statement to check that and if it is a BUY ... write one set of XML, if it is a SELL, write another set of XML and continue on until the EOF ?
I've read some pages and it seems like LINQ is the way ?
if you need more of my code im using i'll post it up.