By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

I tried the indexof and substring and now im getting error again:

P: 69
If im using this code just on a text file i created that containing one long string like "dann123ylip123sharon123daniel" so its working im ggeting first time result : "dann" then "dannylip" then "dannylipsharon"

But now in the code here im using an xml file wich is very big and im using three indexof to get the specific text i want from the the file. Not like im using in the test.txt file.

And when im running the program im getting error on line:

Substring1 = ff.Substring(prevIndex, index - prevIndex);
Telling me that Length cannot be less than zero.
Parameter name: length
And when im putting the mouse on it i see that : index = 368 and prevIndex = 1194945

So what wrong with this code ( formula ) ? I thought it will work on any text file or this xml file.
Cuz when im looking on G the string G in this code its containing the text i wanted from the xml file.

This is the code:

Expand|Select|Wrap|Line Numbers
  1. private void assetid_search()
  2.         {
  3.             string Substring1;
  4.             string Substring2;
  5.             var index = 0;
  6.             var prevIndex = 0;
  7.             var test = string.Empty;
  8.             w=new StreamWriter(@"d:\german.txt");
  9.             while (true)
  10.             {
  11.                 index = tt.IndexOf("Text value", index + 1);
  12.                 if (index == -1)
  13.                 {
  14.                     break;
  15.                 }
  16.                 int t = tt.IndexOf("\"", index);
  17.                 int y = tt.IndexOf(">",t);
  18.                 g = tt.Substring(t,y-t);
  19.                 listBox2.Items.Add(g);
  20.                 Substring1 = ff.Substring(prevIndex, index - prevIndex);
  21.                 string remainingString = ff.Substring(index + g.Length);
  22.                 int remainingStringLenght = remainingString.Contains(g) ? remainingString.IndexOf(g) : remainingString.Length;
  23.                 Substring2 = ff.Substring(index + g.Length, remainingStringLenght);
  24.                 test += Substring1 + Substring2;
  25.                 prevIndex = index + g.Length + remainingStringLenght;
  26.                 w.WriteLine(test);
  27.             }
  28.             w.Close();
  29.         }
  30.  

Thanks.
Mar 22 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.