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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:07:04+00:00 2026-06-06T07:07:04+00:00

I have the following file and I am using an iterator block to parse

  • 0

I have the following file and I am using an iterator block to parse certain re-occuring nodes/parts within the file. I initially used regex to parse the entire file but when certain fields were not present in a node, it would not match. So I am trying to use the yield pattern. The file format is as follows perceeded with the code I am using. All I want from the file are the replicate nodes as an individual part so I can fetch fields within it using a key string and store in collection of objects. I can start parsing where the first replicate occurs but unable to end it where the replicate node ends.

File Format:

X_HEADER
{
    DATA_MANAGEMENT_FIELD_2     NA
    DATA_MANAGEMENT_FIELD_3     NA
    DATA_MANAGEMENT_FIELD_4     NA
    SYSTEM_SOFTWARE_VERSION     NA
}
Y_HEADER
{
    DATA_MANAGEMENT_FIELD_2     NA
    DATA_MANAGEMENT_FIELD_3     NA
    DATA_MANAGEMENT_FIELD_4     NA
    SYSTEM_SOFTWARE_VERSION     NA
}
COMPLETION
{
    NUMBER          877
    VERSION         4
    CALIBRATION_VERSION 1
    CONFIGURATION_ID    877    
}
REPLICATE
{
    REPLICATE_ID            1985
    ASSAY_NUMBER            656
    ASSAY_VERSION           4
    ASSAY_STATUS            Research
    DILUTION_ID         1
}
REPLICATE
{
    REPLICATE_ID            1985
    ASSAY_NUMBER            656
    ASSAY_VERSION           4
    ASSAY_STATUS            Research
}

Code:

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("REPLICATE"))
            {
                yield return current;
                current = new Dictionary<string, string>();
            }
            else
            {
                var parts = line.Split('\t');
            }

            if (current.Count > 0) yield return current;
        }
    }
}

public static void parseFile(string fileName)
    {
        foreach (var part in ReadParts(fileName))
        {
           //part["fIELD1"] will retireve certain values from the REPLICATE PART HERE
        }
    }
  • 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-06T07:07:06+00:00Added an answer on June 6, 2026 at 7:07 am

    If you add a yield return current; after your while loop is over, you will get the final dictionary.

    I believe it would be better to check for ‘}’ as an end to the current block, and then put the yield return there. although you can’t use regex t parse the entire file, you can use regex to search for the key-value pairs within the lines. The following iterator code should work. It will only return dictonaries for REPLICATE blocks.

     // Check for lines that are a key-value pair, separated by whitespace.
    // Note that value is optional
    static string partPattern = @"^(?<Key>\w*)(\s+(?<Value>\.*))?$";
    
    static IEnumerable<IDictionary<string, string>> ReadParts(string path)
    {
        using (var reader = File.OpenText(path))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                // Ignore lines that just contain whitespace
                if (string.IsNullOrWhiteSpace(line)) continue; 
    
                // This is a new replicate block, start a new dictionary
                if (line.Trim().CompareTo("REPLICATE") == 0)
                {
                    yield return parseReplicateBlock(reader);
                }
            }
        }
    }
    
    private static IDictionary<string, string> parseReplicateBlock(StreamReader reader)
    {
        // Make sure we have an opening brace
        VerifyOpening(reader);
        string line;
        var currentDictionary = new Dictionary<string, string>();
        while ((line = reader.ReadLine()) != null)
        {
            // Ignore lines that just contain whitespace
            if (string.IsNullOrWhiteSpace(line)) continue;
    
            line = line.Trim();
    
            // Since our regex used groupings (?<Key> and ?<Value>), 
            // we can do a match and check to see if our groupings 
            // found anything. If they did, extract the key and value. 
            Match m = Regex.Match(line, partPattern);
            if (m.Groups["Key"].Length > 0)
            {
                currentDictionary.Add(m.Groups["Key"].Value, m.Groups["Value"].Value);
            }
            else if (line.CompareTo("}") == 0)
            {
                return currentDictionary;
            }
        }
        // We exited the loop before we found a closing brace, throw an exception
        throw new ApplicationException("Missing closing brace");
    }
    
    private static void VerifyOpening(StreamReader reader)
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            // Ignore lines that just contain whitespace
            if (string.IsNullOrWhiteSpace(line)) continue;
    
            if (line.Trim().CompareTo("{") == 0)
            {
                return;
            }
            else
            {
                throw new ApplicationException("Missing opening brace");
            }
        }
        throw new ApplicationException("Missing opening brace");
    }
    

    Update: I made sure that the regex string includes cases where there is no value. In addition, the group indexes were all changed to use the group name to avoid any issues if the regex string is modified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
I have to write a biginteger into a text file using the following statement.
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have an .aspx file that outputs an image using the following methods: Server.MapPath(somefile.png)
I currently have a class file with the following enumeration: using System; namespace Helper
I want the following functionality using php I have a csv file. Each file
I am attempting to parse an XML file using python expat. I have the
I'm using the following list comprehension: resources = [obj.get(file) for obj in iterator if

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.