Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:39:13+00:00 2026-06-04T00:39:13+00:00

I’ve got a file of blocks of strings, each which end with a certain

  • 0

I’ve got a file of blocks of strings, each which end with a certain keyword. I’ve currently got a stream reader setup which adds each line of the file to a list up until the end of the current block(line contains keyword indicating end of block).

listName.Add(lineFromFile);

Each block contains information e.g. Book bookName, Author AuthorName, Journal JournalName etc. So each block is hypothetically a single item (book, journal, conference etc)..

Now with around 50 or so blocks of information(items) i need some way to store the information so i can manipulate it and store each author(s), Title, pages etc. and know what information goes with what item etc.

While typing this I’ve come up with the idea of possibly storing each Item as an object of a class called ‘Item’, however with potentially several authors, I’m not sure how to achieve this, as i was thinking maybe using a counter to name a variable e.g.

int i = 0;
String Author[i] = "blahblah";
i++;

But as far as i know it’s not allowed? So my question is basically what would be the simplest/easiest way to store each item so that i can manipulate the strings to store each item for use later.

@yamen here’s an example of the file:

Author Bond, james
Author Smith John A
Year 1994
Title For beginners
Book Accounting
Editor Smith Joe
Editor Doe John
Publisher The University of Chicago Press
City Florida, USA
Pages 15-23
End

Author Faux, M
Author Sedge, M
Author McDreamy, L
Author Simbha, D
Year 2000
Title Medical advances in the modern world
Journal Canadian Journal of medicine
Volume 25
Pages 1-26
Issue 2
End


Author McFadden, B
Author Goodrem, G
Title Shape shifting dinosaurs
Conference Ted Vancouver
City Vancouver, Canada
Year 2012
Pages 2-6
End
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:39:14+00:00Added an answer on June 4, 2026 at 12:39 am

    Here is the complete code for this problem.
    It is written with a simple, straight forward approach. It can be optimized, there’s no error checking and the AddData Method can be written in a much more efficient way by using reflection. But it does the job in an elegant way.

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    namespace MutiItemDict
    {
        class MultiDict<TKey, TValue>  // no (collection) base class
        {
            private Dictionary<TKey, List<TValue>> _data = new Dictionary<TKey, List<TValue>>();
    
            public void Add(TKey k, TValue v)
            {
                // can be a optimized a little with TryGetValue, this is for clarity
                if (_data.ContainsKey(k))
                    _data[k].Add(v);
                else
                    _data.Add(k, new List<TValue>() { v });
            }
    
            public List<TValue> GetValues(TKey key)
            {
                if (_data.ContainsKey(key))
                    return _data[key];
                else
                    return new List<TValue>();
            }
        }
    
        class BookItem
        {
            public BookItem()
            {
                Authors = new List<string>();
                Editors = new List<string>();
            }
    
            public int? Year { get; set; }
            public string Title { get; set; }
            public string Book { get; set; }
            public List<string> Authors { get; private set; }
            public List<string> Editors { get; private set; }
            public string Publisher { get; set; }
            public string City { get; set; }
            public int? StartPage { get; set; }
            public int? EndPage { get; set; }
            public int? Issue { get; set; }
            public string Conference { get; set; }
            public string Journal { get; set; }
            public int? Volume { get; set; }
    
            internal void AddPropertyByText(string line)
            {
                string keyword = GetKeyWord(line);
                string data = GetData(line);
                AddData(keyword, data);
            }
    
            private void AddData(string keyword, string data)
            {
                if (keyword == null)
                    return;
    
                // Map the Keywords to the properties (can be done in a more generic way by reflection)
                switch (keyword)
                {
                    case "Year":
                        this.Year = int.Parse(data);
                        break;
                    case "Title":
                        this.Title = data;
                        break;
                    case "Book":
                        this.Book = data;
                        break;
                    case "Author":
                        this.Authors.Add(data);
                        break;
                    case "Editor":
                        this.Editors.Add(data);
                        break;
                    case "Publisher":
                        this.Publisher = data;
                        break;
                    case "City":
                        this.City = data;
                        break;
                    case "Journal":
                        this.Journal = data;
                        break;
                    case "Volume":
                        this.Volume = int.Parse(data);
                        break;
                    case "Pages":
                        this.StartPage = GetStartPage(data);
                        this.EndPage = GetEndPage(data);
                        break;
                    case "Issue":
                        this.Issue = int.Parse(data);
                        break;
                    case "Conference":
                        this.Conference = data;
                        break;
                }
            }
    
            private int GetStartPage(string data)
            {
                string[] pages = data.Split('-');
                return int.Parse(pages[0]);
            }
    
            private int GetEndPage(string data)
            {
                string[] pages = data.Split('-');
                return int.Parse(pages[1]);
            }
    
            private string GetKeyWord(string line)
            {
                string[] words = line.Split(' ');
                if (words.Length == 0)
                    return null;
                else
                    return words[0];
            }
    
            private string GetData(string line)
            {
                string[] words = line.Split(' ');
                if (words.Length < 2)
                    return null;
                else
                    return line.Substring(words[0].Length+1);
            }
        }
    
        class Program
        {
            public static BookItem ReadBookItem(StreamReader streamReader)
            {
                string line = streamReader.ReadLine();
                if (line == null)
                    return null;
    
                BookItem book = new BookItem();
                while (line != "End")
                {
                    book.AddPropertyByText(line);
                    line = streamReader.ReadLine();
                }
                return book;
            }
    
            public static List<BookItem> ReadBooks(string fileName)
            {
                List<BookItem> books = new List<BookItem>();
                using (StreamReader streamReader = new StreamReader(fileName))
                {
                    BookItem book;
                    while ((book = ReadBookItem(streamReader)) != null)
                    {
                        books.Add(book);
                    }
                }
                return books;
            }
    
            static void Main(string[] args)
            {
                string fileName = "../../Data.txt";
                List<BookItem> bookList = ReadBooks(fileName);
    
                MultiDict<string, BookItem> booksByAutor = new MultiDict<string, BookItem>();
                bookList.ForEach(bk =>
                        bk.Authors.ForEach(autor => booksByAutor.Add(autor, bk))
                    );
    
                string author = "Bond, james";
                Console.WriteLine("Books by: " + author);
                foreach (BookItem book in booksByAutor.GetValues(author))
                {
                    Console.WriteLine("    Title : " + book.Title);
                }
    
                Console.WriteLine("");
                Console.WriteLine("Click to continue");
                Console.ReadKey();
            }
        }
    }
    

    And I also want to mention that all the parsing stuff can be avoided if you represent the Data in XML.
    The Data then looks like:

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfBookItem >
      <BookItem>
        <Year>1994</Year>
        <Title>For beginners</Title>
        <Book>Accounting</Book>
        <Authors>
          <string>Bond, james</string>
          <string>Smith John A</string>
        </Authors>
        <Editors>
          <string>Smith Joe</string>
          <string>Doe John</string>
        </Editors>
        <Publisher>The University of Chicago Press</Publisher>
        <City>Florida, USA</City>
        <StartPage>15</StartPage>
        <EndPage>23</EndPage>
      </BookItem>
      <BookItem>
        <Year>2000</Year>
        <Title>Medical advances in the modern world</Title>
        <Authors>
          <string>Faux, M</string>
          <string>Sedge, M</string>
          <string>McDreamy, L</string>
          <string>Simbha, D</string>
        </Authors>
        <StartPage>1</StartPage>
        <EndPage>26</EndPage>
        <Issue>2</Issue>
        <Journal>Canadian Journal of medicine</Journal>
        <Volume>25</Volume>
      </BookItem>
      <BookItem>
        <Year>2012</Year>
        <Title>Shape shifting dinosaurs</Title>
        <Authors>
          <string>McFadden, B</string>
          <string>Goodrem, G</string>
        </Authors>
        <City>Vancouver, Canada</City>
        <StartPage>2</StartPage>
        <EndPage>6</EndPage>
        <Conference>Ted Vancouver</Conference>
      </BookItem>
    </ArrayOfBookItem>
    

    And the code for reading it:

    using (FileStream stream =
        new FileStream(@"../../Data.xml", FileMode.Open,
            FileAccess.Read, FileShare.Read))
            {
                List<BookItem> books1 = (List<BookItem>)serializer.Deserialize(stream);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.