1

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.

JOM
  • 8,139
  • 6
  • 78
  • 111
Pete Szeto
  • 11
  • 2
  • Which column stores whether it is a buy or a sell? also what should the xml difference look like for each type – HELP_ME Jan 07 '13 at 21:44
  • there is a much easier way to create the XML file from the .CSV file. take a look at this posting for a simple answer http://stackoverflow.com/questions/3069661/convert-csv-file-to-xml – MethodMan Jan 07 '13 at 21:55
  • 2
    Please [`stop rolling your own CSV parser`](http://secretgeek.net/csv_trouble.asp)! The line with the `.Split(',')` is something you will regret. – Darin Dimitrov Jan 07 '13 at 22:02

0 Answers0