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/