Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 string
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 28-Feb-13 2:31am
Edited 28-Feb-13 2:32am
v2
Comments
Uday P.Singh at 28-Feb-13 7:34am
   
why not put all your data in xml and retrieve from that
samadhan_kshirsagar at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well I think you should take Uday's suggestion into consideration but if you must read it from text file:
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();
}
</string></string>
 
Do your manipulation when you have your list filled.
 
Good luck,
OI
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Thank you for your quik answears.
 
The file looked like this:
<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.
<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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

this is not question this kind of assignment please send me file i will write code and send to you.
  Permalink  
Comments
Orcun Iyigun at 28-Feb-13 7:49am
   
you are not helping him this way. it is bad for him....

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

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 1 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100