一、项目框架结构
Form1.cs:
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.IO;
namespace testLoadFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnChoose_Click(object sender, EventArgs e)
{
OpenFileDialog fileEds = new OpenFileDialog();
//C# 中 字符串常量可以以 @ 开头声名,转义序列"不"被处理,按"原样"输出
fileEds.InitialDirectory = Application.StartupPath+ @"\Student\";
fileEds.Filter = "xml文件|*.xml";
if (fileEds.ShowDialog() == DialogResult.OK)
{
xmlMsg.Text = fileEds.FileName;
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
#region MyRegion
/* string path = @"C:\\Users\\Administrator\\source\\repos\\testLoadFile\\testLoadFile\\bin\\Debug\\";
FileStream xml = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
string str = txtMsg.Text;
byte[] buff = Encoding.UTF8.GetBytes(str);
xml.Write(buff, 0, buff.Length);
xml.Flush();
xml.Close();*/
#endregion
string path = xmlMsg.Text;
FileStream xml = new FileStream(path,FileMode.Open,FileAccess.ReadWrite);
byte[] buff = new byte[xml.Length];
xml.Read(buff, 0, buff.Length);
string str = Encoding.UTF8.GetString(buff);
Console.WriteLine("xml的内容:{0}",str);
xml.Close();
}
private void btnXmlOriginal_Click(object sender, EventArgs e)
{
XmlCreat studentXml = new XmlCreat();
studentXml.GenerateXml(xmlMsg.Text);
string path = xmlMsg.Text;
StreamReader sr = new StreamReader(xmlMsg.Text);
string str = sr.ReadToEnd();
Console.WriteLine("xml中读取的内容:{0}",str);
sr.Close();
}
private void btnImprove_Click(object sender, EventArgs e)
{
XmlCreat studentXml = new XmlCreat();
//studentXml.GenerateXml(xmlMsg.Text);
//studentXml.AnotherGenerateXml(xmlMsg.Text);
//studentXml.GetXmlInfomation(xmlMsg.Text);
//studentXml.ModifyXmlInformation(xmlMsg.Text);
//studentXml.AddXmlInformation(xmlMsg.Text);
//studentXml.DeleteXmlInformation(xmlMsg.Text);
//studentXml.InsertXmlInformation(xmlMsg.Text);
//studentXml.GetXmlData(xmlMsg.Text);
studentXml.ImproveXml(xmlMsg.Text);
}
}
}
Form1.Designer.cs
namespace testLoadFile
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnLoad = new System.Windows.Forms.Button(