XDT.Extensions 1.1.0

dotnet add package XDT.Extensions --version 1.1.0                
NuGet\Install-Package XDT.Extensions -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XDT.Extensions" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XDT.Extensions --version 1.1.0                
#r "nuget: XDT.Extensions, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XDT.Extensions as a Cake Addin
#addin nuget:?package=XDT.Extensions&version=1.1.0

// Install XDT.Extensions as a Cake Tool
#tool nuget:?package=XDT.Extensions&version=1.1.0                

XDT-Extensions

Package extending the Xml Document Transform XDT.

.NET Core

More about the XDT format and syntax :

https://docs.microsoft.com/en-us/previous-versions/aspnet/dd465326(v=vs.110)?redirectedfrom=MSDN#transform-attribute-syntax

This package overcomes some limitations of XDT around comments insertion and formatting.

It brings new transform functions that enables injecting comments and spaces while inserting elements.

Try it online

You can try and browse the XML document transformations on the Playground.

Nuget Package

.Net Standard 2.1

https://www.nuget.org/packages/XDT.Extensions

Table of content

Use cases

Add a section with comments above

Source XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="IsStaging" value="True" />
    <add key="Environment" value="Dev" />
  </appSettings>
</configuration>

Result XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="IsStaging" value="True" />
    <add key="Environment" value="Dev" />
    
    <add key="storagePath" value="d:/temp/" />
  </appSettings>
</configuration>

Transform XDT

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:xdtExt="xdt-extensions" xml:space="preserve" >
  <xdt:Import assembly="XdtExtensions"
             namespace="XdtExtensions" />

  <appSettings>

    <add key="storagePath" value="d:/temp/" xdt:Transform="InsertIfMissingExt" xdt:Locator="Match(key)">
    <xdtExt:before>
    
    </xdtExt:before>
    </add>
    
  </appSettings>
</configuration>

Insert a section with comments above and below

Source XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="BatchSize" value="100" />
    <add key="IsSandboxed" value="True" />
    <add key="Environment" value="Dev" />
  </appSettings>
</configuration>

Result XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="BatchSize" value="100" />
    
    <add key="newKey" value="newValue" />
    
    <add key="IsSandboxed" value="True" />
    <add key="Environment" value="Dev" />
  </appSettings>
</configuration>

Transform XDT

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:xdtExt="xdt-extensions" xml:space="preserve" >
  <xdt:Import assembly="XdtExtensions"
             namespace="XdtExtensions" />

  <appSettings>

    <add key="newKey" value="newValue" xdt:Transform="InsertBeforeExt(/configuration/appSettings/add[@key='IsSandboxed'])" >
    <xdtExt:before>
    
    </xdtExt:before>
    <xdtExt:after>
    
    </xdtExt:after>
    </add>
    
  </appSettings>
</configuration>

Remove specific comments with XPath

Source XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    
    <add key="IsProduction" value="True" />
    <add key="IsStaging" value="True" />

    

    <add key="Environment" value="Dev" />

    
  </appSettings>
</configuration>

Result XML

<?xml version="1.0"?>
<configuration>
  <appSettings>
    
    <add key="IsProduction" value="True" />
    <add key="IsStaging" value="True" />

    <add key="Environment" value="Dev" />
  </appSettings>
</configuration>

Transform XDT

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns:xdtExt="xdt-extensions" xml:space="preserve" >
  <xdt:Import assembly="XdtExtensions"
             namespace="XdtExtensions" />

  <appSettings>
   <does_not_matter xdt:Transform="RemoveAllExt(/configuration/appSettings/comment()[contains(.,'unwanted comment')])" />
  </appSettings>
</configuration>

Usage

Check the demo project for a complete sample.

// using XdtExtensions.Microsoft.Web.XmlTransform

var xml = File.ReadAllText("samples/source.xml");
var xdt = File.ReadAllText("samples/transform.xml");

using (XmlTransformableDocument document = new XmlTransformableDocument() { PreserveWhitespace = true })
using (XmlTransformation transformation = new XmlTransformation(xdt, isTransformAFile: false, null))
{
    document.LoadXml(xml);

    var success = transformation.Apply(document);
    if (!success)
    {
        throw new Exception($"An error has occurred on apply transform, use IXmlTransformationLogger for more details.");
    }

    document.Save(new MemoryStream());

    Console.WriteLine("Result: \n" + document.OuterXml);
}

Notes

A fork of the XDT package is bundled with the package to better manage spacing and formatting under the namespace XdtExtensions.Microsoft.Web.XmlTransform.

Troubleshooting

If you have an assembly not found error when using the extended transforms, make sure the XdtExtensions assembly is loaded by adding the following in the calling assembly.

private string _loadXdtExtensionsAssembly = XdtExtensions.DefaultNamespace.Namespace;

Extension Transforms Reference

Insert / Replace

All the following transform operations that create content can leverage the meta tags :before and :after to inject content before and/or after the TagElement as shown below.

    <TagElement  xdt:Transform="TransformOperation" />
    <xdtExt:before>
    
    </xdtExt:before>
    <xdtExt:after>
    
    </xdtExt:after>
    </TagElement>
    
Transform Operation Description
InsertExt Same as Insert
InsertIfMissingExt Same as InsertIfMissing
InsertAllExt Insert in all matched locations
InsertBeforeExt Same as InsertBefore
InsertAfterExt Same as InsertAfter
ReplaceExt Same as Replace
ReplaceAllExt Replaces at all matched locations

Remove

The Remove operations can target any node type using XPath as an argument, thus it can be used to remove comments.

Note that the tag and the location do not matter.

<does_not_matter 
     xdt:Transform="RemoveExt(//appSettings/comment()[contains(.,'unwanted comment')])" />

Transform Operation Description
RemoveExt Removes the first matched node
RemoveAllExt Removes all matched nodes

Contributing

We're glad to know you're interested in the project.

Your contributions are welcome !

How can I contribute ?

You can contribute in the following ways :

  • Report an issue / Suggest a feature.
  • Create a pull request.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 414 6/17/2021
1.0.6 487 6/14/2020