Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I utilize StreamWriter to write to a csv file?

So here's what I'm working with. I'm trying to take an XML file, pull the info from the attributes, append them together, and write it to a CSV file. I'm still relatively new to programming, and the other programmer is out of the office today, so I could really use some assistance.

My first question, regards the StringBuilder. Do I need to have an AppendLine at the end of my StringBuilder, so that each string output from the foreach loop is on a new line? And would I need to do that inside the foreach loop?

My second question regards actually writing my string to the CSV file. Would it look something like?

swOutputFile.WriteLine(strAppendedJobData)

And I think this would also go inside the foreach loop, but I'm not too sure.

Thanks for the help, I hope I've worded my question in a somewhat easy to understand manner.

//Create a stream writer to write the data from returned XML job ticket to a new CSV
        StreamWriter swOutputFile;
        string strComma = ",";
        swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));

        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
            //Break down the job data
            string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
            string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
            string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
            string strClient = xeJobUpdate.GetAttribute("Client");
            string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
            string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");

            //Build the string to be added to the new CSV file

            StringBuilder sbConcatJob = new StringBuilder();
            sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
                .Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
                .Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
            string strAppendedJobData = sbConcatJob.ToString();
like image 702
OneFreeFitz Avatar asked Dec 08 '25 12:12

OneFreeFitz


1 Answers

if you want to do it a bit more elegant you could do something like that:

 using(StreamWriter swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read)))
    {
        //Get nodes from returned XML ticket
        XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
        //Pull out data from XML attributes
        foreach (XmlElement xeJobUpdate in xmlJobs)
        {
           List<String> lineItems = new List<String>();
           lineItems.add(xeJobUpdate.GetAttribute("SharpOwlProjectID"));
           //add all the other items

           swOutputFile.WriteLine(String.Join(',', myLine.ToArray()));



        }
    //after the loop you close the writer
   }

    //all the work is done much easier
like image 140
fixagon Avatar answered Dec 10 '25 00:12

fixagon