Save XML File in Project Folder

asp.net c# entity-framework xml

Question

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

I want to store the xml file in a project folder that I made. Then, I'll read from and write to the xml file I made in my folder. Do you know how to do it?

1
3
2/25/2013 5:21:36 PM

Popular Answer

Use System.Reflection.Assembly.GetExecutingAssembly().Location Combining that with will give you your assembly's full path.System.IO.Path.GetDirectoryName() .

It would be similar to:

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

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

However, you should be aware that depending on the security settings of the computer your app has been deployed on, if your application is installed under C:Program Files for example, you'll require some form of elevation permissions to be able to write there. It is recommended to always have a work directory somewhere else, like, say, in (Application Data).

5
2/25/2013 5:48:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow