将字符串加载到Windows c中的xml文档时出错

时间:2013-05-10 07:51:38

标签: c# windows file xmldocument

我有一个应用程序,它读取xml数据,进行一些更改并将其保存到word文档中。当我第一次运行应用程序时,基本URI是“C:\ Documents \ Visual Studio 2010 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ bin \ Debug”。但是,当我第二次运行而没有重新启动应用程序时,基本URI更改为上次保存的位置,我得到一个错误,说明找不到xml文件.Below是代码的一部分。我哪里错了

 string xmlSource = string.Empty;

            if (string.IsNullOrEmpty(xmlSource))

                xmlSource = "Dictionary.xml";

                XmlDocument doc = new XmlDocument();
                doc.Load(xmlSource);
                FileStream usrFs = null;
            try
            {

                usrFs = new FileStream(xmlSource, FileMode.Open, FileAccess.Read,
                                 FileShare.ReadWrite);

            }

            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
            try
            {

                doc.Load(usrFs);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

1 个答案:

答案 0 :(得分:1)

使用完整路径而不是相对路径。

xmlSource = System.AppDomain.CurrentDomain.BaseDirectory + "\\Dictionary.xml";