Click here to Skip to main content
15,911,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to open a text file, compare it with XML file and replace a particular string found in text file with string in XML file.
How to do this in c sharp?

UPDATE from OP:
Done it without using xml file. but i want to do if by using xml file.
following is the code without xml file.How to do it using xml file ?
C#
string textFileName,searchStr,replaceStr;
int backup = 1;
textFileName = lstFilesPrj.Items[i].ToString();
searchStr = "MoveNext";
replaceStr = "counter+1";
string tempFileName = Path.GetTempFileName();
StreamReader sr = null;
sr = new StreamReader(textFileName,Encoding.GetEncoding("windows-1252"));
StreamWriter sw = null;
sw = new StreamWriter(tempFileName, false,Encoding.GetEncoding("windows-1252"));
string line;
System.Text.StringBuilder newline = new
System.Text.StringBuilder();
while ((line = sr.ReadLine()) != null)
{
    string correctString = line.Replace(searchStr,replaceStr);
    sw.WriteLine(correctString);
}
sr.Close();
sw.Close();
if (backup == 1)
{
    if (File.Exists(textFileName + "_bak"))
        File.Delete(textFileName + "_bak");
    File.Move(textFileName, textFileName + "_bak");
}
File.Delete(textFileName);
File.Move(tempFileName, textFileName);
Posted
Updated 28-Feb-11 5:34am
v3
Comments
[no name] 28-Feb-11 0:58am    
And what have you tried so far? What are you having difficulties with? Opening the text file? Reading hr XML? Replacing the string? What?
Sandeep Mewara 28-Feb-11 11:15am    
OP posted his effort.
Albin Abel 28-Feb-11 11:30am    
is it like finding the replace string from xml for the corresponding search string? . If so what is your xml structure

From what I can see you can probably get away with minor modifications to your program.

I would try to use Regular expressions[^] to enhance the search/matching capabilities of my programs.

While you can obviously use the XmlDocument class [^] and process the xml document node for node, I would try a regular expression based approach first, unless you can use System.Xml.Linq[^] to process your matching requirements.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-11 21:05pm    
Good answer, my 5.
--SA
Espen Harlinn 1-Mar-11 12:21pm    
Thank you, SAKryukov :)
I was explaining the principle of KISS to my son this past weekend. Sometimes stepping back from the monitor and thinking around the white board is all it takes to come up with simple solutions.

For all practical purposes xml file is nothing but text file. So, you problem can easily be simplified as working between two text files, irrespective of text file, xml file, source code file etc. The xml file only comes to issue once your start consuming it as xml, but in the case of search/replace it is nothing but text file. The only thing you need to keep in mind is not to break your xml tags.

Think it this way, I can open xml file in my favorite text editor and do some search/replace and save the file. How is that different from what you trying to achieve.

So, if we agree so far, it can easily be through may ways, perhaps regular expression might be good candidate for such task.

Here as some helpers
How to: Search Strings Using Regular Expressions (C# Programming Guide)http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx[^]

C# Demo Application Illustrating the Use of Regular Expressions with The .NET Frameworkhttp://www.regular-expressions.info/dotnetexample.html[^]
 
Share this answer
 
v2
Comments
Espen Harlinn 28-Feb-11 11:58am    
More or less what I had in mind, my 5 :)
Sergey Alexandrovich Kryukov 2-Mar-11 4:00am    
Very good, my 5.
--SA

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