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 7177231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:40:09+00:00 2026-05-28T16:40:09+00:00

I want to know if it is possible to read from a text file

  • 0

I want to know if it is possible to read from a text file in a faster and smarter way.

This is a typical format of my data in a text file:

Call this “part”:

ID:1;
FIELD1 :someText;
FIELD2 :someText;
FIELD3 :someText;
FIELD4 :someText;
FIELD5 :someText;
FIELD6 :someText;
FIELD7 :someText;
FIELD8 :someText;
END_ID :
01: someData;
02: someData;
...
...
48: someData;
ENDCARD:

I have thousands of them in a text file.

Is it possible to use LINQ to read it “part” by “part”? I don’t want to loop through every single line.

Will it be possible for LINQ to start at ID:1; and end at ENDCARD:?

The reason for this is that i want to create a object for every “part”…

I had something like this in mind:

string[] lines = System.IO.File.ReadAllLines(SomeFilePath);

//Cleaning up the text file of unwanted text
var cleanedUpLines = from line in lines
                     where !line.StartsWith("FIELD1")
                     && !line.StartsWith("FIELD5")
                     && !line.StartsWith("FIELD8")
                     select line.Split(':');

//Here i want to LINQtoText "part" by "part"

//This i do not want to do!!!
foreach (string[] line in cleanedUpLines)
{
}
  • 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-05-28T16:40:10+00:00Added an answer on May 28, 2026 at 4:40 pm

    Here you go:

    static void Main()
    {
        foreach(var part in ReadParts("Raw.txt"))
        {   // all the fields for the part are available; I'm just showing
            // one of them for illustration
            Console.WriteLine(part["ID"]);
        }
    }
    
    static IEnumerable<IDictionary<string,string>> ReadParts(string path)
    {
        using(var reader = File.OpenText(path))
        {
            var current = new Dictionary<string, string>();
            string line;
            while((line = reader.ReadLine()) != null)
            {
                if(string.IsNullOrWhiteSpace(line)) continue;
                if(line.StartsWith("ENDCARD:"))
                {
                    yield return current;
                    current = new Dictionary<string, string>();
                } else
                {
                    var parts = line.Split(':');
                    current[parts[0].Trim()] = parts[1].Trim().TrimEnd(';');
                }
            }
            if (current.Count > 0) yield return current;
        }
    }
    

    What this does is: create an iterator block (a state machine that reads and “yields” data as it is iterated; it does not read the entire file in one go) that scans the lines; if it is the end of a card, the card is “yielded”; otherwise it adds the data into a dictionary for storage.

    Note: if you have your own class that represents the data, then you could use something like reflection or FastMember to set the values by name.

    This does not use LINQ directly; however, it is implemented as an enumerable sequence, which is the building block of LINQ-to-Objects, so you could consume this with LINQ, i.e.

    var data = ReadParts("some.file").Skip(2).First(x => x["ID"] == "123");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if it is possible to stream data from the server
I have a CLI script and want it to read data from a file.
I want to know if it is possible to call an activity through background
Here's what I want the program to do: Read a text file (the text
Basically I want to stream data from memory into a tar/gz format (possibly multiple
I want to know if this is even possible. One of my requirement is
i want know if is possible, to get a specific element value of a
i want to know if it possible to access the browser bookmarks via Flash.
I am creating a CMS and want to know if its possible to download
Basically, I just want to know if its possible to use Nhibernate to migrate

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.