2016-06-01 62 views
0

我想複製我的bin/debug/project文件夾中的現有.xml文檔,並創建一個新的XML文檔作爲具有不同名稱的副本。c#將現有的XML文檔與Linq複製到XML

這裏是我到目前爲止已經試過:

 XDocument ReleasesXML; 

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

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

回答

1

大約只用File如何功能?

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

如果你只需要複製該文件,你也可以這樣寫:

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

+0

是EA的療法方法,我可以使用項目路徑?我記得在vba你可以做一些像CurrentPath –

+0

編輯我的回答 –

+0

什麼樣的項目你編碼?網絡或應用程序? –

0

在我的ASP.NET項目,這是正確的代碼:

// 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");