Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I'm quit new to C# and I resentle stucked with some code and I would be very pleased to get some help.

I want to be able to take out a line from a .txt file with just knowing what the text is on the line above. For exemple:

Name:
Olof
LastName:
Wiking
Country:
Sweden

And for exemple "Name:" will not stand on the same line all the time, it will variat from time to time.

I hope you understand my bad English.

best regards
Victor
Posted
Updated 26-Dec-17 1:00am
v2
Comments
Uday P.Singh 28-Feb-13 7:34am    
why not put all your data in xml and retrieve from that
samadhan_kshirsagar 28-Feb-13 7:41am    
its very easy......first of all check the file is present or not...then open the file.....and read the character from the file

Well I think you should take Uday's suggestion into consideration but if you must read it from text file:
C#
string line = string.Empty;
List<string> lines = new List<string>();
if(File.Exists(path))
{
    // Read the file and display it line by line.
    System.IO.StreamReader file = new System.IO.StreamReader("c:\\yourtextfilepathhere.txt");
    while((line = file.ReadLine()) != null)
    {
       lines.Add(line);
    }
    file.Close();
}


Do your manipulation when you have your list filled.

Good luck,
OI
 
Share this answer
 
v2
Comments
Nakhia_ind 26-Dec-17 9:25am    
Thanks a good code .But here line should be a string variable first declare
Thank you for your quik answears.

The file looked like this:
XML
<TD width="20%" class="WG3">Name&nbsp;</TD>
        <TD width="20%" class="WG3">Olof&nbsp;</TD>




I wrote the code like this and its works perfect.
C#
<pre lang="cs">public string getMeetInfo(string fileContent , string searchMeetInfo)
{



    string searchString1 = ">" + searchMeetInfo + "&nbsp;</TD>";
    int IntRubrikStart = fileContent.IndexOf(searchString1);
    string searchString2 = ">";
    int intWantedNameStart = fileContent.IndexOf(searchString2, IntRubrikStart + searchString1.Length) + 1;
    string searchString3 = "&nbsp";
    int intWantedNameEnd = fileContent.IndexOf(searchString3, intWantedNameStart);
    string strOut = fileContent.Substring(intWantedNameStart, intWantedNameEnd - intWantedNameStart);

    return strOut;



Output:
Olof
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900