/// <summary> /// Deserialize the file defined in the constructor /// </summary> /// <returns>A Document containing the deserialized values</returns> public Document Deserialize() { Document doc = new Document(); int index = 0; while (!File.EndOfStream) { Row row = new Row(); char[] line = File.ReadLine().ToCharArray(); char[] buffer = new char[line.Length]; //temporary buffer for composing values int bufferIndex = 0; for (int i = 0; i < line.Length; i++) { if (line[i] == '\"') { int closingQuoteIndex = IndexOf(line, '\"', i + 1); //copies all the stuff between the double quotes in the buffer if (closingQuoteIndex == -1) throw new CSVFileExcepion("Double quote mismatched"); for (int a = i + 1; a < closingQuoteIndex; a++) { buffer[bufferIndex] = line[a]; ++bufferIndex; } i = closingQuoteIndex; //the index is now the closing double quote, next loop it will be the , } else if (line[i] == ',') //when it sees a comma, it dumps the content of the buffer in the row { DumpBuffer(row, buffer); buffer = new char[line.Length]; //clears the array after dumping values bufferIndex = 0; } //TODO: deal with the space else { buffer[bufferIndex] = line[i]; ++bufferIndex; } } DumpBuffer(row, buffer); //the last column must be added if (index == 0) //the first row is the header doc.SetHeader(row); else doc.AddRow(row); ++index; } return doc; }
/// <summary> /// Constructor that requires a Document and the path of the file to be serialized /// </summary> /// <param name="Document">The document to be serialize</param> /// <param name="FilePath">Path of the file to be serialized</param> public Serializer(Document Document, String FilePath) { this.Document = Document; this.FilePath = FilePath; }