0

Here is C# method in the xslt file that compiles without errors:

  <msxsl:script language="C#" implements-prefix="user">
    <msxsl:assembly name="System.Core" />
    <msxsl:using namespace="System.IO" />
    <msxsl:using namespace="System.Collections.Generic"/>
    <msxsl:using namespace="System.Text.RegularExpressions" />
    <msxsl:using namespace="System.Linq" />
    <![CDATA[
    public string GetDesc(string state, string licNum, string date)
    {
       return string.Join("; ", new[]{ "State: " + state, "DL Number: " + licNum, "Date: " + 
       date }.Where(s => !string.IsNullOrWhiteSpace(s)));
    }
        ]]>
    </msxsl:script>

however if I change it to expression-bodied method

public string GetDesc(string state, string licNum, string date) => 
string.Join("; ", new[]{ "State: " + state, "DL Number: " + licNum, "Date: " + date }
.Where(s => !string.IsNullOrWhiteSpace(s)));

the following message shows and ten errors in the output:

error

Is it possible to use this syntax?

Andrei Karcheuski
  • 3,116
  • 3
  • 38
  • 39

2 Answers2

1

The latest MSXML was released way before C# 6, so all these new language features are not supported.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
1

It should be possible to install the NuGet package https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/, that way XslCompiledTransform tries to use the Roslyn C# CodeDomProvider with the newer C# features.

However, somehow the installation and search paths don't match so, as https://stackoverflow.com/a/40311406/252228 explains, the compiler is looked for in bin\roslyn while it is installed in roslyn.

If I use the first option "move the \roslyn subdirectory to \bin\roslyn" (I did that by hand for the test), then C# code like your use of => inside msxsl:script compiles and runs with the .NET framework and XslCompiledTransform.

I tried that with the latest version 3.6.0 of the package named above and a C# .NET framework 4.7.2 console application run in VS 2019.

There is also the package https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix/ that is supposed to be installed in addition to the DotNetCompilerPlatform one to fix the installation path but somehow for me with VS 2019 and the above setup it didn't fix the problem, so the only successful test I have been able to perform is to move the directory with Windows Explorer.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110