在windows c#中加载字符串到xml文档时出错

本文关键字:xml 文档 出错 字符串 windows 加载 | 更新日期: 2023-09-27 18:13:03

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

 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);
            }

在windows c#中加载字符串到xml文档时出错

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

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