2016-06-01 61 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");