title: XML file copy issues c# "XML document must have a top level element. Error processing resource "
body: <p>In my program, I am checking to see if an xml file exists. If the file is not there, I simply create it in the specified directory, then attempt to copy the contents of another xml into the new xml file. Likewise if the file exists, I will copy the contents of another file and overwrite the existing file.  When I run my application and check the directory, the file that I want outside xml code copied too says "XML document must have a top level element. Error processing resource".</p>
  
  <p>So far I have tried : System.IO.File.Copy(sourceFile, targetPath); for the file copying.</p>
  
  <p>my code block looks similar to this: </p>
  
  <pre><code>string sourceFile= "C:\\fileIWantToCopy.xml;
  string targetpath= "C:\\NeedsFilledWithSourceContents.xml;
  
  if (File.Exists(targetPath) == false) {
      File.Create(targetPath);
      System.IO.File.Copy(sourceFile, targetPath, true);
  } else {
      System.IO.File.Copy(sourceFile, targetPath, true);
  }
  
  XDoc.Save(String.Format(targetPath));
  </code></pre>
  
  <p>Again I just need some tips on how I can copy the contents of one xml file to another fresh, newly created one without the "XML document must have a top level element. Error processing resource" error. My top line of the source xml doc is :</p>
  
  <p>&lt; ? xml version="1.0" encoding="utf-8" ? > </p>
  
  <p>then proceeds to have typical head/body construction. </p>
  
  <p>Do I need to write content to the new file before I can copy anything over to it?</p>
  
  <p>Thanks</p>