c# - 将 XML 文件保存在项目文件夹中

标签 c# asp.net xml entity-framework

try
{
    XElement contactsFromFile = XElement.Load("App_Data/EmployeeFinList.xml");
    var xEle = new XElement("Employees",
        from emp in ListFromBasicPay
        select new XElement("Employee",
            new XAttribute("EmpID", emp.employee_personal_id),
            new XElement("GrandTotal", emp.grandTotal),
            new XElement("Housing", emp.housing),
            new XElement("BasePay", emp.base_pay),
            new XElement("XchangeRate", emp.Exchange_rate)));

    xEle.Save("..\\changesetDB.xml");

    Debug.WriteLine("Converted to XML");
}
catch (Exception ex)
{
    Debug.WriteLine(ex.Message);
}

我想将 xml 文件保存在我在项目中创建的文件夹中。然后我将使用在我的文件夹中创建的那个 xml 文件并从中读取和写入。知道怎么做吗?

最佳答案

使用 System.Reflection.Assembly.GetExecutingAssembly().Location 要获取程序集的完整路径,请将其与 System.IO.Path.GetDirectoryName() 结合使用。

那就是:

String path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

xEle.Save(path + @"\myfilename.xml");

虽然您应该注意,如果您的应用程序安装在 C:\Program Files 中,您将需要某种提升权限才能在那里写入,具体取决于您的应用程序已部署的计算机的安全设置在。最好总是在其他一些位置有一个工作目录,例如(应用程序数据)..

关于c# - 将 XML 文件保存在项目文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15072557/

相关文章:

c# - WebService 和 Remoting 之间的区别

c# - 带计时器的进度条

c# - 从 DetailsView 获取 BoundField 的值

xml - 为什么 normalize-space() 不去除所有空格?

javascript - 从 XML 文件中分离出一个结果

c# - FirstOrDefault() 在迭代中添加天数的时间

c# - 如何在类里面再次调用 "static void Main(string[] args) "

添加元素时 Java XML DOM 错误

asp.net - 编码练习 : how to avoid hard coding?

asp.net - 什么是输出缓存,使用输出缓存来改善Web应用程序的性能始终是一件好事吗?