Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //csvParser.cs
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Microsoft.VisualBasic.FileIO;
- using Product = ProductHelper.Product;
- using csvWriter = cWriter.csvWriter;
- namespace cParser {
- public class csvParser {
- private List<string> _handle;
- private List<string> _title;
- private List<string> _body;
- private List<string> _vendor;
- private List<string> _type;
- private List<string> _tags;
- private List<string> _mehrwertSteuer;
- private List<string> _status;
- private List<string> _seoTitel;
- private List<string> _seoBeschreibung;
- private List<string> _versandGewicht;
- private List<string> _nameOption1;
- private List<string> _wertOption1;
- private List<string> _nameOption2;
- private List<string> _wertOption2;
- private List<string> _nameOption3;
- private List<string> _wertOption3;
- private List<string> _sku;
- private List<string> _preis;
- private List<string> _einkaufsPreis;
- private List<string> _barcode;
- private List<string> _aufLager;
- private List<string> _stringiantenId;
- private List<string> _productId;
- private List<Product> _products;
- private string path;
- private bool read;
- private void readCsv() {
- using TextFieldParser cParser = new TextFieldParser(path);
- cParser.CommentTokens = new string[] { "#" };
- cParser.SetDelimiters(new string[] { "," });
- cParser.HasFieldsEnclosedInQuotes = true;
- // Skip the row with the column names
- cParser.ReadLine();
- while (!cParser.EndOfData) {
- // Read current line fields, pointer moves to the next line.
- string[] fields = cParser.ReadFields();
- this._handle.Add(fields[0]);
- this._title.Add(fields[1]);
- this._body.Add(fields[2]);
- this._vendor.Add(fields[3]);
- this._type.Add(fields[4]);
- this._tags.Add(fields[5]);
- this._mehrwertSteuer.Add(fields[6]);
- this._status.Add(fields[7]);
- this._seoTitel.Add(fields[8]);
- this._seoBeschreibung.Add(fields[9]);
- this._versandGewicht.Add(fields[10]);
- this._nameOption1.Add(fields[11]);
- this._wertOption1.Add(fields[12]);
- this._nameOption2.Add(fields[13]);
- this._wertOption2.Add(fields[14]);
- this._nameOption3.Add(fields[15]);
- this._wertOption3.Add(fields[16]);
- this._sku.Add(fields[17]);
- this._preis.Add(fields[18]);
- this._einkaufsPreis.Add(fields[19]);
- this._barcode.Add(fields[20]);
- this._aufLager.Add(fields[21]);
- this._stringiantenId.Add(fields[22]);
- this._productId.Add(fields[23]);
- }
- this.read = true;
- }
- private void addHandle() {
- List<string> tList = new List<string>();
- this._handle.ForEach(tList.Add);
- foreach (string handle in tList) {
- string temp = handle.ToLower().Replace(" ", "-");
- this._handle.Remove(handle);
- this._handle.Add(temp);
- }
- this.addVendor();
- }
- private void addVendor() {
- // Assuming that the vendor is the Word before the first spacing
- List<string> tList = new List<string>();
- this._vendor.ForEach(tList.Add);
- foreach (string vendor in tList) {
- string temp = vendor.Split(" ")[0];
- this._vendor.Remove(vendor);
- this._vendor.Add(temp);
- }
- this.addTags();
- }
- private void addTags() {
- // Example: 187 ELLO & RAFFA => 187,ello,&,raffa
- List<string> tList = new List<string>();
- this._tags.ForEach(tList.Add);
- foreach (string tag in tList) {
- string temp = tag.ToLower().Replace(" ", ",");
- this._tags.Remove(tag);
- this._tags.Add(temp);
- }
- replacePublished();
- }
- private void replacePublished() {
- // ?? requires a "Published" column (equal to our Status)
- for (int i = 0; i < this._status.Count(); i++) {
- if (this._status[i] == "YES") {
- this._status[i] = "TRUE";
- }
- else{
- this._status[i] = "FALSE";
- }
- }
- for (int i = 0; i < this._title.Count; i++)
- {
- Product product = new Product(_handle.ElementAt(i), _title.ElementAt(i), _body.ElementAt(i),
- _vendor.ElementAt(i),
- _type.ElementAt(i), _tags.ElementAt(i), _status.ElementAt(i), _nameOption1.ElementAt(i),
- _wertOption1.ElementAt(i),
- _nameOption2.ElementAt(i), _wertOption2.ElementAt(i), _nameOption3.ElementAt(i),
- _wertOption3.ElementAt(i),
- _sku.ElementAt(i), _versandGewicht.ElementAt(i), "shopify", _aufLager.ElementAt(i), "deny",
- "manual",
- _preis.ElementAt(i), "", _barcode.ElementAt(i).ToString(), "", _title.ElementAt(i), _seoTitel.ElementAt(0),
- _seoBeschreibung.ElementAt(i),
- "", "", _einkaufsPreis.ElementAt(i));
- this._products.Add(product);
- }
- }
- public void start(){
- this.readCsv();
- while (!read) ;
- this.addHandle();
- }
- public void printEverything() {
- Console.Write("handle: ");
- this._handle.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("title: ");
- this._title.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("body: ");
- this._body.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("vendor: ");
- this._vendor.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("type: ");
- this._type.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("tags: ");
- this._tags.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("tax: ");
- this._mehrwertSteuer.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("status: ");
- this._status.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("seoTitle: ");
- this._seoTitel.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("seoDescription: ");
- this._seoBeschreibung.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("weight: ");
- this._versandGewicht.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("nameOption1: ");
- this._nameOption1.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("valueOption1: ");
- this._wertOption1.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("nameOption2: ");
- this._nameOption2.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("valueOption2: ");
- this._wertOption2.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("nameOption3: ");
- this._nameOption3.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("valueOption3: ");
- this._wertOption3.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("sku: ");
- this._sku.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("price: ");
- this._preis.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("purchasingPrice: ");
- this._einkaufsPreis.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("barcode: ");
- this._barcode.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("inStock: ");
- this._aufLager.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("stringiantId: ");
- this._stringiantenId.ForEach(Console.Write);
- Console.WriteLine();
- Console.Write("productId: ");
- this._productId.ForEach(Console.Write);
- Console.WriteLine();
- Console.WriteLine("--------------------products start--------------------");
- foreach (Product p in this._products) {
- p.printEverything();
- }
- Console.WriteLine("---------------------products end---------------------");
- }
- public csvParser() {
- this.path = @"killme.csv";
- this._handle = new List<string>();
- this._title = new List<string>();
- this._body = new List<string>();
- this._vendor = new List<string>();
- this._type = new List<string>();
- this._tags = new List<string>();
- this._mehrwertSteuer = new List<string>();
- this._status = new List<string>();
- this._seoTitel = new List<string>();
- this._seoBeschreibung = new List<string>();
- this._versandGewicht = new List<string>();
- this._nameOption1 = new List<string>();
- this._wertOption1 = new List<string>();
- this._nameOption2 = new List<string>();
- this._wertOption2 = new List<string>();
- this._nameOption3 = new List<string>();
- this._wertOption3 = new List<string>();
- this._sku = new List<string>();
- this._preis = new List<string>();
- this._einkaufsPreis = new List<string>();
- this._barcode = new List<string>();
- this._aufLager = new List<string>();
- this._stringiantenId = new List<string>();
- this._productId = new List<string>();
- this._products = new List<Product>();
- this.read = false;
- }
- public csvParser(string path) {
- this.path = path;
- this._handle = new List<string>();
- this._title = new List<string>();
- this._body = new List<string>();
- this._vendor = new List<string>();
- this._type = new List<string>();
- this._tags = new List<string>();
- this._mehrwertSteuer = new List<string>();
- this._status = new List<string>();
- this._seoTitel = new List<string>();
- this._seoBeschreibung = new List<string>();
- this._versandGewicht = new List<string>();
- this._nameOption1 = new List<string>();
- this._wertOption1 = new List<string>();
- this._nameOption2 = new List<string>();
- this._wertOption2 = new List<string>();
- this._nameOption3 = new List<string>();
- this._wertOption3 = new List<string>();
- this._sku = new List<string>();
- this._preis = new List<string>();
- this._einkaufsPreis = new List<string>();
- this._barcode = new List<string>();
- this._aufLager = new List<string>();
- this._stringiantenId = new List<string>();
- this._productId = new List<string>();
- this._products = new List<Product>();
- this.read = false;
- }
- ~csvParser() {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement