一、思路流程
1、界面设计
a.文件路径
b.xml 关键字
c.关键字内查找内容
d.替换内容
e.日志打印
2、执行流程
a.判断路径、关键字、查找内容、替换内容是否为空,并给出提示;
b.根据路径判断该文件夹下是否有 xml 文件
c.关键字内查找内容
d.替换内容
e.日志打印
1、界面设计
a.文件路径
b.xml 关键字
c.关键字内查找内容
d.替换内容
e.日志打印
2、执行流程
a.判断路径、关键字、查找内容、替换内容是否为空,并给出提示;
b.根据路径判断该文件夹下是否有 xml 文件
c.关键字内查找内容
d.替换内容
e.日志打印
2、代码模块
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Threading;
namespace Change_XML
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void start_path_Click(object sender, EventArgs e)
{
FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.text_path.Text = path.SelectedPath;
}
private void start_change_Click(object sender, EventArgs e)
{
string date_time = DateTime.Now.ToLongTimeString().ToString();
string textpath = this.text_path.Text;
try
{
if (string.IsNullOrEmpty(textpath) || string.IsNullOrEmpty(this.keyword.Text) || string.IsNullOrEmpty(this.find_text.Text) || string.IsNullOrEmpty(this.replace_text.Text)) //判断 textpath 是否为空
{
string notice_text = "注意:请检查文件路径/替换文本/替换为是否均已填写...";
MessageBox.Show("注意:请检查文件路径/替换文本/替换为是否均已填写...", "提示");
this.list_logs.Items.Add(date_time + " ==> " + notice_text);
list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。
}
else
{
DirectoryInfo dir = new DirectoryInfo(textpath);
var fileInfos = dir.GetFiles();
if (fileInfos.Length == 0)
{
string notice_text = "注意:该文件夹下没有文件!";
this.list_logs.Items.Add(date_time + " ==> " + notice_text);
list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。
MessageBox.Show(notice_text, "提示");
}
else
{
int count_file = 0;
int count_all = 0;
foreach (var fileInfo in fileInfos)
{
string xml_path = this.text_path.Text + "/" + fileInfo.Name.ToString();
this.list_logs.Items.Add(date_time + " ==> " + xml_path);
list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。
XmlDocument doc = new XmlDocument();
doc.Load(xml_path);
XmlNodeList xn = doc.SelectSingleNode("doc").ChildNodes;
foreach (XmlElement xn1 in xn)
{
XmlElement xe = (XmlElement)xn1;
this.list_logs.Items.Add(date_time + " ==> " + xe.Name.ToString());
list_logs.TopIndex = list_logs.Items.Count - 1;
if( xe.Name.ToString() == this.keyword.Text )
{
if (this.find_text.Text == "#all#")
{
xe.InnerText = this.replace_text.Text;
count_file++;
}
else
{
string old_value = xe.InnerText;
if (old_value.IndexOf(this.find_text.Text)>=0)
{
count_file++;
string new_value = old_value.Replace(this.find_text.Text, this.replace_text.Text);
xe.InnerText = new_value;
this.list_logs.Items.Add(date_time + " ==> " + xe.InnerText);
list_logs.TopIndex = list_logs.Items.Count - 1;
}
}
}
}
count_all++;
doc.Save(xml_path);
int.TryParse(this.sleep_time.Text, out int time_num);
Thread.Sleep(time_num); //休眠时间
}
string notice_text = "总计"+count_all+"个文件,已修改"+count_file+"个文件!";
this.list_logs.Items.Add(date_time + " ==> " + notice_text);
list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。
MessageBox.Show(notice_text, "提示");
}
}
}
catch (Exception ex)
{
this.list_logs.Items.Add(date_time + " ==> " + ex.ToString());
list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。
MessageBox.Show(ex.ToString(), "提示");
}
}
}
}
四、下载地址
点击跳转百度云,提取码: k4s5

