How to Import Text file with Feild names in each line to Hashtable ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Veeky
    New Member
    • Oct 2010
    • 1

    How to Import Text file with Feild names in each line to Hashtable ?

    Hi All,

    I am very new in C# and am doing a test project. In my project i m using a text file which has records along with feild name in each line. How can i import the data in a Hashtable ??

    Text File Example :

    First_Name Joe
    Last_Name Smith
    Cell 9785466885
    Email joesmith@xyz.co m

    First_Name Amanda
    Last_Name Collin
    Cell 4485965755
    Email amandacollin@gs tock.co

    ........ and so on...

    i want to take just the values from each line and store in my hashtable.

    Please Help !!

    Thanks !
    Veeky
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    What do you plan to key this hash table with? I'd personally suggest a Dictionary<stri ng, string> instead of a HashTable, because of the type safety, it is much easier to work with.

    You'll need to read the contents of the text file. My favorite tool for this is a StreamReader. There's an example of its use on the page. Read each line into a List<string>.

    If you're trying to split each line around the space, then you'll need to .Split each string in your array.

    After that, you'll have each entry. Just add them to the Dictionary/HashTable.

    Comment

    • Veeky Trivedi

      #3
      RE : Import Text file with Feild names in each line to Hashtable ?

      hi Curtis,

      Thanks for the quick response... but is it possible for you to give me some example code... for doing this...

      Thanks,

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        All the example code you need is in the pages I linked. Most MSDN pages include example code.

        Comment

        Working...