2 liuxinxin86 liuxinxin86 于 2013.09.17 09:24 提问

winform C# 修改XML文件

在winform中form中的文本框中修改都取的XML文件,修改后,保存时判断修改后的XML是否符合XML基本的格式规范,最后一步的判断如何实现呢?

namespace ManageXMLFile

{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path;
private void Form1_Load(object sender, EventArgs e)
{
path = Path.GetDirectoryName(this.GetType().Assembly.Location) + "\Config";
DirectoryInfo directories = Directory.CreateDirectory(path);
FileInfo[] files=directories.GetFiles();
lbFileNames.Items.AddRange(files);
if (files.Length > 0)
{
lbFileNames.SetSelected(0, true);
}
}
string selectedFile;
private void lbFileNames_SelectedIndexChanged(object sender, EventArgs e)
{
selectedFile = lbFileNames.SelectedItem + "";
txtXMLContext.Visible = false;
webBrowserXMLContext.Visible = true;
webBrowserXMLContext.Url = new Uri(path + "\" + selectedFile);
btnUpdate.Text = "修改";
}

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        Button btn=(Button)sender;
        if (btn.Text == "修改")
        {
            btn.Text = "保存";
            txtXMLContext.Visible = true;
            webBrowserXMLContext.Visible = false;
            string selectedFileText = File.ReadAllText(path + "\\" + selectedFile);
            txtpath.Text = path + "\\" + selectedFile;
            txtXMLContext.Text = selectedFileText;
        }
        else
        {
            btn.Text = "修改";
           txtXMLContext.Visible = false;
            webBrowserXMLContext.Visible = true;
            string saveXMLContext = txtXMLContext.Text;
            File.WriteAllText(path + "\\" + selectedFile, saveXMLContext);
            webBrowserXMLContext.Url = new Uri(path + "\\" + selectedFile);
        }
    }


}

}

Csdn user default icon
上传中...
上传图片
插入图片