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

I want to save the xml file in a folder i created in my project. I will then use that xml file created in my folder and read and write from it. Any idea how to do it?

11
  • Do you want to add it to you solution directories under your project????
    – Zaid Amir
    Feb 25, 2013 at 17:18
  • Yes! I have two projects in my solution, one for the UIs and the other containing my linq to entities clasess . i want to save it in my solution directory Feb 25, 2013 at 17:20
  • what is the exact path where you want to save this xEle.Save("..\\changesetDB.xml"); normally you should be able to do the following xEle.Save("changesetDB.xml"); but you should capture the Path of where you are trying to save this file
    – MethodMan
    Feb 25, 2013 at 17:21
  • i was just playing around with that, if i use xEle.Save("changesetDB.xml"); .. are you saying that will work? how do i capture the path? Feb 25, 2013 at 17:22
  • A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll .. i get this error when i use xEle.Save("changesetDB.xml") Feb 25, 2013 at 17:24

2 Answers 2

5

Use System.Reflection.Assembly.GetExecutingAssembly().Location To get the full path of you assembly, Combine that with System.IO.Path.GetDirectoryName().

That would be like:

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

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

Though you should note that if your application is installed in C:\Program Files for example, you'll need some sort of elevation permissions to be able to write there depending on the security settings of the machine your app has been deployed on. It is best to always have a work directory in some other location like (Application Data) for example..

-1

Use Red Serpent's answer for getting your project's folder:

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

Then use:

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

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

You can then use myXMLPath to read and write from XML file you just created.

1
  • what is myFolder?? what is SavePath??
    – yusuf
    Sep 25, 2013 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.