将选定的对话框项目获取到另一个按钮方法

Getting selected dialog item to another button method

所以我正在编写一个 windows 表单应用程序,用户将从他的计算机 select 一个 xml 文件(使用文件对话框),当他单击保存按钮时,它应该保存在数据库中。但是我对如何将 selected 项目获取到保存按钮方法有点迷茫。以下是我的btnChooseFile_click

private void btnChooseFile_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Multiselect = false;
        dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            tbxXmlFile.Text = dlg.FileName;
            XmlDocument invDoc = new XmlDocument();
            invDoc.Load(dlg.FileName);
            ....
            ....
        }
    }

下面是我的btnStore_click

  private void btnStore_Click(object sender, EventArgs e)
    {
        try
        {
            string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
            using (SqlConnection sqlConn = new SqlConnection(cs))
            {
                DataSet ds = new DataSet();
                ds.ReadXml("This is where I want to get the selected file");
                ....
                ....
            }
        }
    }

那我该怎么做呢?

可以使用私有成员变量

private String filename = null;

private void btnChooseFile_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Multiselect = false;
    dlg.Filter = "XML documents (*.xml)|*.xml|All Files|*.*";

    if (dlg.ShowDialog() == DialogResult.OK)
    {
        tbxXmlFile.Text = dlg.FileName;
        XmlDocument invDoc = new XmlDocument();
        invDoc.Load(dlg.FileName);
        ....
        ....
        this.filename = dlg.FileName;
    }
}

private void btnStore_Click(object sender, EventArgs e)
{
    try
    {
        string cs = @"Data Source=localhost;Initial Catalog=db;integrated security=true;";
        using (SqlConnection sqlConn = new SqlConnection(cs))
        {
            DataSet ds = new DataSet();
            ds.ReadXml(this.filename);
            ....
            ....
        }
    }
}