如何将文本文件与xml文件进行比较并替换特定的字符串? [英] How to compare a text file with xml file and replace a particular string ?

查看:94
本文介绍了如何将文本文件与xml文件进行比较并替换特定的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文本文件,将其与XML文件进行比较,然后将文本文件中找到的特定字符串替换为XML文件中的字符串.
如何在C Sharp中执行此操作?

OP的更新:
无需使用xml文件即可完成.但是我想通过使用xml文件来做.
以下是没有xml文件的代码.如何使用xml文件?

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 ?

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);

推荐答案

据我所知,您可能只需对程序进行少量修改即可.

我会尝试使用正则表达式 [ XmlDocument类 [ System.Xml.Linq [^ ]处理您的匹配要求.

问候
Espen Harlinn
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


这个周末我向儿子解释了KISS的原理.有时候,从显示器上退后一步,想一想白板是想出简单解决方案的全部.

出于所有实际目的,xml文件不过是文本文件.因此,您的问题可以轻松地简化为在两个文本文件之间工作,而与文本文件,xml文件,源代码文件等无关.仅当您开始将其作为xml使用时,xml文件才会发布,但在search/替换它只是文本文件.您唯一需要记住的是不要破坏xml标记.

这样想,我可以在我最喜欢的文本编辑器中打开xml文件,并进行一些搜索/替换并保存该文件.这与您要实现的目标有何不同?

因此,如果到目前为止我们都同意,可以很容易地通过可能的方式进行,也许正则表达式可能是完成此任务的合适人选.
这里是一些帮手
如何:使用正则表达式搜索字符串(C#编程指南)http://msdn.microsoft.com/en-us/library/ms228595(v=vs.80).aspx [说明在.NET Framework中使用正则表达式的C#演示应用程序http://www.regular-expressions.info/dotnetexample.html [ ^ ]
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[^]


这篇关于如何将文本文件与xml文件进行比较并替换特定的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆