I am creating an application in c# for replacing a string in a file.
The input to the application are the file that I want to replace and a xml file that contains the actual line and its replacement line.I am reading the xml file and if my file contains the line which is present in the xml,it gets replaced;
I am replacing the first occurrence of the string.
private void Replace_Click(object sender, EventArgs e)
string dir = @"D:\Output";
string path = XmltextBox.Text;
string txtpath = Filetextbox.Text;
string file = Path.Combine(dir, "output.txt");
System.Xml.XmlDocument docxml = new System.Xml.XmlDocument();
int i = 0;
string str = null;
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
xmlnode = docxml.GetElementsByTagName("rule");
for (i = 0; i <= xmlnode.Count - 1; i++)
if (i == 1)
FileStream tempFile = new FileStream(despath, FileMode.Open);
StreamReader Templine = new StreamReader(tempFile16);
while ((line = Templine.ReadLine()) != null)
string linevalue = xmlnode.ChildNodes.Item(1).InnerText.Trim();
string replace16 ="currentClip.stopDrag();"
string replaceVariable = ReplaceFirstOccurence(text,actual, replace);
FileStream filewrite = new FileStream(despath, FileMode.Create);
StreamWriter filew = new StreamWriter(filewrite);
For Eg: I want to replace the line "stopDrag();" with "currentClip.stopDrag();"
If my file contains "stopDrag();" only once then it is working fine.But if my file contains "stopDrag();" twice then it is getting replaced as "currentClip.currentClip.stopDrag();"
If there are 3 occurrences then it is getting replaced 3 times.
How to avoid such replacements?