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.Xml;
using System.Diagnostics;
using System.Collections;
using System.IO;
namespace MZD_TransferLine
{
public partial class Form1 : Form
{
public List<List<String>> MZD_ImageName;//无盘镜像名称
public List<List<String>> MZD_WorkGroup;//无盘工作组
public int Start_Position;//起始位置
public int End_Position;//结束位置
public String Groupid_id_values=String.Empty;//组的id
public String image1_id_values=string.Empty;//镜像id
public List<int> XML_Rework_Position;//需要修正的座标
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
ofd.Title = "配置文件";
ofd.Filter = "AllFiles|*.XML";
textBox1.Text = ofd.FileName;
if (ofd.FileName != null && ofd.FileName != String.Empty)//读取无盘组起始~结束位置
{
if (Read_Xml_Position(ofd.FileName, @"<group>", @"</group>"))
{
MZD_WorkGroup = new List<List<String>>();
comboBox1.Items.Clear();
if (Read_Xml_Data(ofd.FileName, Start_Position, End_Position, @"id value", @"name value", comboBox1,MZD_WorkGroup))
{
button1.Enabled = false;
comboBox1.Enabled = true;
comboBox1.Focus();
}
}
}
}
public String GetIdValues(List<List<String>> Source_Data, String Data_Name)
{
Boolean Flag = true;
foreach (List<String> ii in Source_Data)
{
if (ii[1] == Data_Name)
{
return ii[0];
}
}
return null;
}
public Boolean Read_Xml_Data(String PathFileName, int Start_Position, int End_Position,
String Item_Value,String Item_Name,ComboBox Item,List<List<String>>Item_Values)//读取Xml数据
{
Boolean Flag = true;
if (File.Exists(PathFileName))
{
try
{
String Temp = String.Empty;
int n = 0;
List<String> Temp_Buffer=new List<string>();
Temp_Buffer.Add("NULL");
Temp_Buffer.Add("NULL");
Temp = File.ReadAllText(PathFileName);
String[] Array_Red = Temp.Split(new String[] {"\n"},StringSplitOptions.RemoveEmptyEntries);
foreach(String ss in Array_Red)
{
if (n >= Start_Position && n <= End_Position)
{
if (ss.Contains(Item_Value))
{
String[] Array_str = ss.Split(new String[] { "=", "\"" }, StringSplitOptions.RemoveEmptyEntries);
//Temp_Buffer.Add(Array_str[1]);
Temp_Buffer[0] = Array_str[1];
}
else if (ss.Contains(Item_Name))
{
String[] Array_str = ss.Split(new String[] { "=", "\"" }, StringSplitOptions.RemoveEmptyEntries);
//Temp_Buffer.Add(Array_str[1]);
Temp_Buffer[1] = Array_str[1];
Item.Items.Add(Array_str[1]);
Item_Values.Add(Temp_Buffer);
Temp_Buffer = new List<string>();
Temp_Buffer.Add("NULL");
Temp_Buffer.Add("NULL");
}
}
n++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
Flag = false;
}
}
else
{
MessageBox.Show(PathFileName + " File Not Fund!!", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
public Boolean Read_Xml_Position(String PathFileName,String Start_Str,String End_Str)//读取xml文件
{
Boolean Flag = true;
Start_Position = 0;
End_Position = 0;
if (File.Exists(PathFileName))
{
FileStream fs = new FileStream(PathFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.Default);
Boolean Is_Start_Position = false;
try
{
String Temp = String.Empty;
int n = 0;
while ((Temp = sr.ReadLine()) != null)
{
if (!Is_Start_Position)
{
if (Temp.Contains(Start_Str))
{
Start_Position = n;
Is_Start_Position = true;
}
}
if (Temp.Contains(End_Str))
End_Position = n;
n++;
}
}
catch (Exception ex)
{
Flag = false;
MessageBox.Show(ex.Message,"系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
sr.Close();
fs.Close();
}
}
else
{
MessageBox.Show(PathFileName+" File Not Fund!!","系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)//无盘项目组选择
{
if (comboBox1.Text != null && comboBox1.Text != String.Empty && comboBox1.Text != "")
{
if (Read_Xml_Position(textBox1.Text, @"<image>", @"</image>"))
{
MZD_ImageName = new List<List<String>>();
comboBox2.Items.Clear();
if (Read_Xml_Data(textBox1.Text, Start_Position, End_Position, @"id value", @"name value", comboBox2, MZD_ImageName))
{
Groupid_id_values = GetIdValues(MZD_WorkGroup, comboBox1.Text);
if (Groupid_id_values!=null)//读取无盘项目组ID
{
comboBox1.Enabled = false;
comboBox2.Enabled = true;
comboBox2.Focus();
}
}
}
}
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)//选择镜像文件
{
if (comboBox2.Text != null && comboBox2.Text != String.Empty && comboBox2.Text != "")
{
image1_id_values= GetIdValues(MZD_ImageName, comboBox2.Text);
if (image1_id_values != null)//读取项目id
{
comboBox2.Enabled = false;
button2.Enabled = true;
button2.Focus();
}
}
}
public Boolean EditFile(int curLine, String newLineValue, String PathName)//修正文件信息
{
Boolean Flag = true;
if (File.Exists(PathName))
{
String Temp = String.Empty;
Temp = File.ReadAllText(PathName);
StringBuilder sb = new StringBuilder();
String[] Array_Str = Temp.Split(new String[] {"\n"},StringSplitOptions.RemoveEmptyEntries);
int n = 0;
foreach (String ss in Array_Str)
{
if (n != curLine)
{
sb.Append(ss + "\n");
}
else
sb.Append(newLineValue+"\n");
n++;
}
FileStream fs1 = new FileStream(PathName,FileMode.Open,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs1);
try
{
sw.Write(sb.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
Flag = false;
}
finally
{
sw.Close();
fs1.Close();
}
}
else
{
MessageBox.Show("Not Fund "+PathName+" File!!");
Flag = false;
}
return Flag;
}
public Boolean Get_All_ReworkPosition(String FileName,String Querying_ID)//获取所有需要修正的ID
{
Boolean Flag = true;
if (File.Exists(FileName))
{
XML_Rework_Position = new List<int>();
String Temp = String.Empty;
Temp = File.ReadAllText(FileName);
String[] Array_Str = Temp.Split(new String[] {"\n"},StringSplitOptions.RemoveEmptyEntries);
int n = 0;
foreach (String dd in Array_Str)
{
if (dd.Contains(Querying_ID))
{
XML_Rework_Position.Add(n+1);
}
n++;
}
}
else
{
MessageBox.Show("Not Find "+FileName+" File!!","系统提醒",MessageBoxButtons.OK,MessageBoxIcon.Error);
Flag = false;
}
return Flag;
}
private void Button2_Click(object sender, EventArgs e)//设置参数
{
Boolean IsPass = true;
if (Get_All_ReworkPosition(textBox1.Text, @"<groupid value=" + "\"" + Groupid_id_values + "\"" + @" />"))
{
foreach (int i in XML_Rework_Position)
{
if (!EditFile(i, "\t\t\t"+@"<image1_id value=" + "\"" + image1_id_values + "\"" + @" />", textBox1.Text))
IsPass = false;
}
if (IsPass)
{
MessageBox.Show("完成转线无盘镜像更换!!","系统提醒",MessageBoxButtons.OK);
button2.Enabled = false;
comboBox1.Text = "";
comboBox2.Text = "";
textBox1.Text = "";
button1.Enabled = true;
button1.Focus();
}
}
}
}
}
C# 编辑修改xml文件
最新推荐文章于 2024-09-01 13:25:58 发布