0

I want to copy an existing .xml document inside of my bin/debug/ project Folder and create a new XML document being just a copy with an different name.

Here is what i have tried so far:

        XDocument ReleasesXML;

        if (XDocument.Load(id + ".xml") == null)
        {
            XDocument Version1 = XDocument.Load("SourcefileReleases.xml");
            ReleasesXML = new XDocument(Version1);

        }
        else
        {
            ReleasesXML = XDocument.Load(id + ".xml");
        }

3 Answers 3

2

If you only need to copy the file you could also write:

    string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
    string fileFrom = System.IO.Path.Combine(path, "from.xml");
    string fileTo = System.IO.Path.Combine(path, "to.xml");     
    Systen.IO.File.Copy(fileFrom, fileTo);

Reference

Sign up to request clarification or add additional context in comments.

2 Comments

is ther ea way i could use the Project path? I remember in vba u can do something like CurrentPath
What kind of project are you coding? Web or Application?
1

How about using just File functions?

if(File.Exists(id + ".xml"))
{
    File.Copy("SourcefileReleases.Xml", "newfile");  
}
else
{
    // logic
}

Comments

0

In my ASP.NET project this is the right code :

  // AppDomain.CurrentDomain.BaseDirectory = your project path

   XDocument Version1 = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "/Data/fileversion1.xml");
   XDocument  newFile = new XDocument(Version1); 
   //Save the file with a new name
   newFile.Save(AppDomain.CurrentDomain.BaseDirectory + "/Data/fileversion2.xml");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.