Question

How to read the details of particular sections using c#...

i am new to c# and i have to read the details from a text file based on the sections marked between square brackets "[]".. the file looks like

[Header]
This is the header info for the file
[Body]
This is the body information for the provided file
and it contains many information for the file
[Summary]
Summary for the file.

i need to read each of these sections details (eg. [Header], [Body])..

any help in this direction is highly appreciated...

Was it helpful?

Solution

Assuming the text between those headers does not contain brackets you can do it this way:

Dictionary<String,String> content = new Dictionary<String,String>();

String text = @"[Header]
This is the header info for the file
[Body]This is the body information for the provided file and it contains many information for the file
[Summary]Summary for the file.";

foreach (String section in text.Split("[".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
    String[] sectionParts = section.Split(']');
    content.Add(sectionParts[0], sectionParts[1]);
}

The Dictionary will contain the content of your File as header-text-pairs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top