Csv Parser : CSV « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » CSVScreenshots 
Csv Parser
        
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.IO;
using System.Text;

namespace hollyathome.utilities
{
    public class CsvParser
    {
        private List<List<string>> dataRows = new List<List<string>>();
        private Encoding encoding;

        public CsvParser(Stream csvStream, Encoding encoding)
        {
            StreamReader csvReader = new StreamReader(csvStream, encoding);
            ParseCsvStream(csvReader);
            this.encoding = encoding;
        }

        public string GetValue(int row, int col)
        {
            return dataRows[row][col];
        }

        public void SetValue(int row, int col, string value)
        {
            dataRows[row][col= value;
        }

        public void ParseCsvStream(StreamReader textReader)
        {
            bool inQuotedString = false;
            int lineLength = 0;
            int charPos = 0;
            List<string> dataRow = new List<string>();
            List<char> data = new List<char>();
            string input;

            while ((input = textReader.ReadLine()) != null)
            {
                lineLength = input.Length;

                //Loop the input line char by char
                for (charPos = 0; charPos < lineLength; charPos++)
                {
                    char curChar = input[charPos];

                    if (charPos == && curChar == ',')
                        dataRow.Add(string.Empty);
                    else if (charPos > && curChar == ',' && !inQuotedString)
                    {
                        dataRow.Add(new String(data.ToArray<char>()));
                        data = new List<char>();
                    }
                    else if (curChar == '"')
                    {
                        data.Add(curChar);
                        inQuotedString = !inQuotedString;
                    }
                    else
                        data.Add(curChar);
                }

                //I'm at the end of the line, but the newline might be part of the data. If so replace the 
                //newline that ReadLine swallowed earlier
                if (inQuotedString)
                {
                    data.Add('\r');
                    data.Add('\n');
                }
                else //Otherwise store the row, start a fresh row and carry on.
                {
                    dataRow.Add(new String(data.ToArray<char>()));
                    data = new List<char>();

                    dataRows.Add(dataRow);
                    dataRow = new List<string>();
                }
            }
        }

        public void Save(string path)
        {
            StreamWriter textWriter = new StreamWriter(path, false, encoding);
            string seperator = "";

            foreach (List<string> row in dataRows)
            {
                foreach (string data in row)
                {
                    textWriter.Write(String.Format("{0}{1}", seperator, data));

                    if (seperator == "")
                        seperator = ",";
                }

                seperator = "";
                textWriter.Write(Environment.NewLine);
            }

            textWriter.Close();
        }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Builds a bracketed CSV list from the array
2.Builds a CSV list from the specified String[], separator string and quote string.
3.Builds a CSV list from the specified int[], separator String and quote String
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.