Open sandboxFocusImprove this doc

PS0205: Invalid Expression in Configuration File

Severity Error
Reported by PostSharp
Message Invalid XPath expression "<Expression>": <Reason>

Cause

Many attributes of a postsharp.config file, such as the Value of a Property element or a Condition attribute, accept expressions. The text between braces, for example {$Configuration = 'Debug'}, is an XPath 1.0 expression, in which properties are variables. See Using Expressions in Configuration Files.

PostSharp reports PS0205 when the XPath engine of .NET cannot compile or evaluate the expression. <Expression> is the text between the braces, and <Reason> is the explanation that .NET gives. The usual causes are operators of C# that XPath does not have: == instead of =, || instead of or, or ! instead of not(). PostSharp then ignores the element whose condition failed, or gives no value to the attribute.

In the following example, the condition that applies the aspect in the Debug and Release configurations combines its two comparisons with ||.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0205_Error;assembly:PS0205_Error">
    <!-- XPath combines conditions with or, not with ||. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0205_Error.OrderService" AttributeTargetMembers="PlaceOrder" Condition="{$Configuration = 'Debug' || $Configuration = 'Release'}" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0205_Error;

// The postsharp.config file applies this aspect to OrderService in the Debug and Release configurations.
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
error PS0205: postsharp.config(5,115): Invalid XPath expression "$Configuration = 'Debug' || $Configuration = 'Release'": Expression must evaluate to a node-set.

Resolution

Write the expression in XPath 1.0: compare with = and !=, combine conditions with and and or, negate them with not(), and quote strings with single quotes inside an XML attribute in double quotes. See Using Expressions in Configuration Files for the functions that PostSharp adds to XPath.

In the following example, the condition uses the or operator, and the aspect is applied.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:app="clr-namespace:Errors.PS0205_Fixed;assembly:PS0205_Fixed">
    <!-- The condition uses the or operator of XPath. -->
    <app:LogAttribute AttributeTargetTypes="Errors.PS0205_Fixed.OrderService" AttributeTargetMembers="PlaceOrder" Condition="{$Configuration = 'Debug' or $Configuration = 'Release'}" />
  </Multicast>
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;

namespace Errors.PS0205_Fixed;

// The postsharp.config file applies this aspect to OrderService in the Debug and Release configurations.
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine( $"Entering {args.Method.Name}." );
    }
}

public class OrderService
{
    public void PlaceOrder( string product )
    {
        Console.WriteLine( $"Order placed for {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Coffee" );
    }
}
Entering PlaceOrder.
Order placed for Coffee.

See Also

Other Resources

Using Expressions in Configuration Files
Configuration File Schema Reference
Adding Aspects Using XML