private static void PerformXdtTransform(IPackageFile file, string targetPath, IProjectSystem projectSystem) { if (projectSystem.FileExists(targetPath)) { string content = Preprocessor.Process(file, projectSystem); try { using (var transformation = new XmlTransformation(content, isTransformAFile: false, logger: null)) { using (var document = new XmlTransformableDocument()) { document.PreserveWhitespace = true; // make sure we close the input stream immediately so that we can override // the file below when we save to it. using (var inputStream = projectSystem.OpenFile(targetPath)) { document.Load(inputStream); } bool succeeded = transformation.Apply(document); if (succeeded) { using (var memoryStream = new MemoryStream()) { // save the result into a memoryStream first so that if there is any // exception during document.Save(), the original file won't be truncated. document.Save(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); using (var fileStream = projectSystem.CreateFile(targetPath)) { memoryStream.CopyTo(fileStream); } } } } } } catch (Exception exception) { throw new InvalidDataException( String.Format( CultureInfo.CurrentCulture, NuGetResources.XdtError + " " + exception.Message, targetPath, projectSystem.ProjectName), exception); } } }
private static void PerformXdtTransform(IPackageFile file, string targetPath, IProjectSystem projectSystem) { if (projectSystem.FileExists(targetPath)) { string transform = Preprocessor.Process(file, projectSystem); try { using (XmlTransformation transformation = new XmlTransformation(transform, false, null)) { using (XmlTransformableDocument document = new XmlTransformableDocument()) { document.PreserveWhitespace = true; using (Stream stream = projectSystem.OpenFile(targetPath)) { document.Load(stream); } if (transformation.Apply(document)) { using (MemoryStream stream2 = new MemoryStream()) { document.Save(stream2); stream2.Seek(0L, SeekOrigin.Begin); using (Stream stream3 = projectSystem.CreateFile(targetPath)) { stream2.CopyTo(stream3); } } } } } } catch (Exception exception) { object[] args = new object[] { targetPath, projectSystem.ProjectName }; throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, NuGetResources.XdtError + " " + exception.Message, args), exception); } } }