2013-02-25 67 views
3
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文件并从中读取和写入。任何想法如何做到这一点?在项目文件夹中保存XML文件

+0

你想把它添加到你的项目下的解决方案目录吗? – 2013-02-25 17:18:32

+0

是的!我在我的解决方案中有两个项目,一个用于UI,另一个包含我的linq到实体分支。我想将它保存在我的解决方案目录中 – 2013-02-25 17:20:14

+0

什么是您想要保存这个'xEle.Save(“.. \\ changesetDB.xml”)的确切路径;''通常您应该能够执行以下操作 'xEle .Save(“changesetDB.xml”);'但是你应该捕获你试图保存这个文件的路径 – MethodMan 2013-02-25 17:21:27

回答

4

使用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文件,例如,你需要某种高度的权限,以便能够在里面写取决于您的应用程序已被部署到的机器的安全设置。这是最好的总是有一些其他位置一样(应用程序数据),例如工作目录..

-1

用红蛇的答案让你的项目的文件夹:

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

然后使用:

string mySavePath = Path.Combine(path, myFolder); 

    string myXMLPath = Path.Combine(SavePath,"changesetDB.xml"); 

然后,您可以使用myXMLPath从您刚刚创建的XML文件读取和写入数据。

+0

什么是myFolder?什么是SavePath? – yusuf 2013-09-25 18:58:41

相关问题