sayedihashimi / slow-cheetah

XML Transforms for app.config and other XML files
Other
242 stars 54 forks source link

Error when transforming config root node containing a namespace #186

Closed bobthayer closed 7 years ago

bobthayer commented 8 years ago

Transforming the root node of a config file that contains a namespace fails with this error: Could not write Destination file: Object reference not set to an instance of an object.

This transform will cause the exception to be thrown:

<?xml version="1.0"?>
<configuration xmlns:config="urn:test:configuration" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" rootAttribute="someValue" xdt:Transform="SetAttributes(rootAttribute)">
  <appSettings>
    <add key="setting01" value="debug01"
         xdt:Locator="Match(key)" xdt:Transform="Replace" />    
    <add key="setting02" value="debug02"
         xdt:Locator="Match(key)" xdt:Transform="Replace" />
  </appSettings>
</configuration>

I think this is related to the Microsoft.Web.Xdt package. The transform executes successfully, but the exception is thrown on document.Save(destination)

    public class Transformer : ITransformer {
        public void Transform(string source, string transform, string destination) {
            if (string.IsNullOrWhiteSpace(source)) { throw new ArgumentNullException("source"); }
            if (string.IsNullOrWhiteSpace(transform)) { throw new ArgumentNullException("transform"); }
            if (string.IsNullOrWhiteSpace(destination)) { throw new ArgumentNullException("destination"); }

            if (!File.Exists(source)) {
                throw new FileNotFoundException("File to transform not found", source);
            }
            if (!File.Exists(transform)) {
                throw new FileNotFoundException("Transform file not found", transform);
            }

            using (XmlTransformableDocument document = new XmlTransformableDocument())
            using (XmlTransformation transformation = new XmlTransformation(transform)) {
                document.PreserveWhitespace = true;
                document.Load(source);

                var success = transformation.Apply(document);
                if (!success) {
                    string message = string.Format(
                        "There was an unknown error trying while trying to apply the transform. Source file='{0}',Transform='{1}', Destination='{2}'",
                        source,transform,destination);
                    throw new TransformFailedException(message);
                }

                // Error occurs here
                // Could not write Destination file: Object reference not set to an instance of an object.
                document.Save(destination);
            }
        }

I downloaded the SlowCheetah project and updated the Microsoft.Web.Xdt nuget package to the latest version. That stopped the error from happening.

I was going to try to fix the project and submit a pull request, but I am not familiar with Nuget package projects or Visual Studio extension projects. SlowCheetah.Xdt.dll has a dependency on an older version of Microsoft.Web.XmlTransform.

davilimap commented 7 years ago

I have tried this with the new version (2.5.48) and root node transforms are working. If you are still experiencing this after updating, reopen the issue and add steps to reproduce this from a new project.