c# stream reader reading text file into richtextbox inbetween [] tags -


i have text file want read richtextbox on form using button here text file: [1] hello text file. reading richtextbox. want multiple rows textbox display on 1 button click. [/1]

here code:

private void button2_click(object sender, eventargs e) {     using (streamreader reader = new streamreader(@"f:\test.txt"))     {         bool content = false;         while ((line = reader.readline()) != null)         {             if (line.contains("[1]"))             {                 content = true;             }              if (content == true)             {                 txtcontent.appendtext(reader.readline());             }              if (line.contains("[/1]"))             {                 content = false;                 break;                 //txtcontent.appendtext(reader.readline());             }         }     } } 

when click button2 adds first line

how can read text in between [1] , [/1]

i have looked use of xml text file have alot of data in end ive tried avoid using it. go onto using same richtextbox store text inbetween [2] , [/2] on button click

thanks

your logic looks may skips lines.

you read line in file , if contains [1] set flag. check if flag set , read line file. won't include read line. check if next line contains[/1] , break reading loop.

say read first line , "[1]hello text file." logic not include "hello text file" in richtextbox, because you've requested next line read. check end tag ([/1]), result in false. come top of loop , read next line ("i reading richtextbox."). fail first check, , content flag still true current line not added richtextbox because did readline().

now question stated beginning tag ([1]) on line itself. if that's true, continue loop after set content flag true.

if don't want tags [1] , [/1] not in richtextbox replace them in line variable , add line variable richtextbox. don't read line add richtextbox, use line read in if meets condition of being between tags.

this snippet should handle (generic formats) of file, unless tags appear again within tags (for example, "[1][1][/1][/1]")

private void button2_click(object sender, eventargs e) {      using (streamreader reader = new streamreader (@"f:\test.txt"))      {          bool content = false;           while ((line = reader.readline()) != null)          {             if (line.contains("[1]"))             {                 content = true;                 // need continue if on line                 continue;             }             if (content == true)             {                 // replace should remove tags , add what's left richtextbox                 txtcontent.appendtext(line.replace("[1]", "").replace("[/1]", ""));             }             if(line.contains("[/1]"))             {                 content = false;                 break;              }         }     } } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -