Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm looking for some help but I'm completely stuck. I'm trying to create a Dictionary Class that gets populated from a CSV file stored on my hard drive an works within a WebService, so I can input a word and the meaning is the output(all of which is in the CSV file) Can anyone help with that? I've been looking online but I can't find anything that's worked for me so far. And I'm trying to do this in a webservice.

This is the code I have;

So far I have this;

[WebMethod]
string line;
StreamReader reader = new StreamReader("myfile.txt"); //writes to file
[WebMethod]
public class
{
private Dictionary<string,> _regionTimeValues = new Dictionary<string,>();
private String _words;


}

I know it's not a lot but after I figure out how to populate the webservice from using the CSV file as a dictionary I should be good to go.

Thank You.
Posted
Updated 21-Nov-13 3:02am
v4
Comments
johannesnestler 21-Nov-13 5:32am    
hmm, hard to tell where you got stuck. Steps involved: Read the csv-file, optionally create objects representing the data from the csv. add them to a dictionary with unique keys (ideally the data has some "identity"), use the dictionary data (depends on your Service)
Member 10327451 21-Nov-13 6:36am    
So far I have this;

[WebMethod]
string line;
StreamReader reader = new StreamReader("myfile.txt"); //writes to file
[WebMethod]
public class
{
private Dictionary<string,> _regionTimeValues = new Dictionary<string,>();
private String _words;


}

but from here I'm stuck. I've been using tutorials I found online but they're not helping.
johannesnestler 21-Nov-13 7:25am    
Maybe update your question...
Anyway, so you have Problems:
1. How to read and Interpret csv
2. how to add a list of (lines) to your dict
3. How to key your values(ß)

See my (upcoming) example ...
Member 10327451 21-Nov-13 8:45am    
Hi, Thanks for the answer below. I'm just going to run it and have a look. Will this also work with a WebService though?
johannesnestler 21-Nov-13 8:53am    
There should be no difference for this code if run from a Service, but of course this depends on your environment (file access, ..)

1 solution

Hi,

have a look at this runnable example. feel free to ask any questions...

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ReadCSVToDictionary
{
    class Program
    {
        // Wrapper class to represent the information in one line of the csv
        class LineInformation
        {
            public string Key { get; set; }
            public string StringValue { get; set; }
            public int IntValue { get; set; }
            public DateTime DateTimeValue { get; set; }
        }

        static void Main(string[] args)
        {
            // Replace with path to your data file
            string strPathToCSVFile = @"myfile.csv";

            #region Create example file

            List<lineinformation> listExample = new List<lineinformation>();
            listExample.Add(new LineInformation { Key = "value1", StringValue = "blabla", IntValue = 1, DateTimeValue = DateTime.Now });
            listExample.Add(new LineInformation { Key = "value2", StringValue = "ahahah", IntValue = 44, DateTimeValue = DateTime.Now.AddHours(1) });
            listExample.Add(new LineInformation { Key = "value3", StringValue = "what?", IntValue = 5519, DateTimeValue = DateTime.Now.AddDays(1) });

            StringBuilder sbFileContent = new StringBuilder();
            foreach (LineInformation li in listExample)
            {
                sbFileContent.AppendFormat("{0};{1};{2};{3}" + Environment.NewLine, li.Key, li.StringValue, li.IntValue, li.DateTimeValue);
            }
            File.WriteAllText(strPathToCSVFile, sbFileContent.ToString());

            #endregion // Create example file

            // Read all lines from file
            string[] astrLines = File.ReadAllLines(strPathToCSVFile);

            // Go through all lines
            const char chSeparator = ';';
            Dictionary<string,> dict = new Dictionary<string,>();
            foreach (string strLine in astrLines)
            {
                // Split the lines by the used separator sign (comma, tab, ...)
                string[] astrSplit = strLine.Split(chSeparator);

                // create and intialize an instance of the type representing the information
                // from the csv file - in our example a LineInformation object.
                // In real you have to know how the csv columns are ordered and formatted (culture specific?)
                LineInformation lineinformation = new LineInformation
                {
                    Key = astrSplit[0], // we assume first column of csv holds the key
                    StringValue = astrSplit[1], // ... second column is our string value
                    IntValue = Convert.ToInt32(astrSplit[2]), // ... third column is an integer - we also convert it's value
                    DateTimeValue = DateTime.Parse(astrSplit[3])
                };

                // Add the instance to our Dictionary
                dict.Add(lineinformation.Key, lineinformation);
            }

            // use your dictionary here
            foreach(KeyValuePair<string,> kvp in dict)
            {
                Console.WriteLine("{0};{1};{2};{3}", kvp.Value.Key, kvp.Value.StringValue, kvp.Value.IntValue, kvp.Value.DateTimeValue);
            }

            Console.ReadKey();
        }


    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900