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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:31:52+00:00 2026-05-31T21:31:52+00:00

I’m trying to read a text document that is separated into essentially paragraphs of

  • 0

I’m trying to read a text document that is separated into essentially “paragraphs” of info that all have identically formatted headers.

The goal is to add each of the header titles as parents to a treeView and the rest of the header info as children to the treeView.

Adding the headers is easy, however I’m having problems adding the children to the correct parent. I’m using the StreamReader to read each line, find the start of a “paragraph” and create the parent. The children I need to add are going to be on the next three lines. How to I the currentLine from the StreamReader's ReadLine()? Or is there a better way to do this?

using (var sr = new StreamReader(file))
{
     string line;
     while ((line = sr.ReadLine()) != null)
     {
         //Sheet name
         if (line.Contains("Sheet Name"))
         {
             string parentNodeName = line.Split('=')[1].Trim();

              //Add Parent
              treeView.Nodes.Add(parentNodeName);
              //Add children, on next 3 lines
              treeView.Nodes[parentNodeName].Nodes.Add("Child-1 Text"); //on next line
              treeView.Nodes[parentNodeName].Nodes.Add("Child-2 Text"); //on next next line
              treeView.Nodes[parentNodeName].Nodes.Add("Child-3 Text"); //etc
              }
          }
      sr.Close();
      sr.Dispose();
 }

Sample text (“paragraph” header) from file:

800: Sheet Name = Sheet3
999  : Process  = 2
1000 : Material = AL,CR,STEEL
1001 : Cut Quality = 0,1,2,3,4,5,6,7,8

Am going to parse these lines to be added to the tree, adding children to identical parent nodes. Like:

Sheet3
    2
        AL,CR,STEEL
              0,1,2,3,4,5,6,7,8 
        SS
              0,1,2,3,4,5,6,7,8 
        MS
              0,1,2,3,4,5 
    4
        AL,CR,STEEL
              0,1,2,3,4,5,6,7,8 
        MS
              0,1,2,3,4,5
    10
        AL,CR,STEEL
              0,1,2,3,4,5,6,7,8
        SS
              0,1,2,3,4,5,6,7,8   
  • 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-31T21:31:53+00:00Added an answer on May 31, 2026 at 9:31 pm

    Here is sample how you can read file and build TreeView.

    using (var reader = new StreamReader(file))
    {
        while (!reader.EndOfStream)
            AddParagraphToTree(ReadParagraph(reader));
    }     
    

    Frankly speaking it’s better to split those two operations – parsing file and building tree. But for quick sample will go. Also consider that Dispose() will be called automatically at the end of using block.

    private class Paragraph
    {
        public string SheetName { get; set; }
        public string Process { get; set; }
        public string Material { get; set; }
        public string CutQuality { get; set; }
    }
    

    Here we parse file. I suppose file is exactly in expected format. No errors handling, no format checking. You free to add some 🙂

    private Paragraph ReadParagraph(StreamReader reader)
    {
        Paragraph paragraph = new Paragraph();
        paragraph.SheetName = ReadNextValue(reader);
        paragraph.Process = ReadNextValue(reader);
        paragraph.Material = ReadNextValue(reader);
        paragraph.CutQuality = ReadNextValue(reader);
        return paragraph;
    }
    
    private string ReadNextValue(StreamReader reader)
    {
        return reader.ReadLine().Split('=')[1].Trim();
    }
    

    And here we add parsed paragraphs to tree.

    private void AddParagraphToTree(Paragraph paragraph)
    {
        var sheetNode = AddNode(treeView.Nodes, paragraph.SheetName);
        var processNode = AddNode(sheetNode.Nodes, paragraph.Process);
        var materialNode = AddNode(processNode.Nodes, paragraph.Material);
        AddNode(materialNode.Nodes, paragraph.CutQuality);
    }
    
    private TreeNode AddNode(TreeNodeCollection nodes, string key)
    {
        if (!nodes.ContainsKey(key))
            nodes.Add(new TreeNode(key) { Name = key });
    
        return nodes[key];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

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.