title: Enable XPath2 queries in System.Xml.XPath (XPathException: invalid token)
body: <p>Microsofts System.Xml.XPath nuget-package, available for .NET 4.6, claims to support XPath 1.0 and 2.0. De <a href="https://msdn.microsoft.com/en-us/library/system.xml.xpath(v=vs.110).aspx" rel="nofollow noreferrer">documentation</a> says descibes the namespace:</p>
  
  <p><em>The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the XQuery 1.0 and XPath 2.0 Data Model.</em> </p>
  
  <p>After upgrading Visual Studio, upgrading and all my projects to framework version 4.6 I still can't get the simplest XPath-2.0 for-expression to work. According to the <a href="https://www.w3.org/TR/xpath20/#id-for-expressions" rel="nofollow noreferrer">specification</a>, they should work. </p>
  
  <p>I can't imagine Microsoft claiming to support something it actually doesn't, so obviously I'm doing something wrong. <strong>How do I correctly use an XPath2 query?</strong></p>
  
  <pre><code>[TestMethod]
  public void TestXPath2()
  {
      // The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the 
      // XQuery 1.0 and XPath 2.0 Data Model.
  
      var expression = "for $x in /Root/Foo/Bar return $x";
      var compiledExpression = System.Xml.XPath.XPathExpression.Compile(expression); 
      // throws XPathException: "for ... has an invalid token"
  }
  </code></pre>
  
  <p>P.S.
  What I actually want, is to get something like this to work:</p>
  
  <pre><code>    [TestMethod]
      public void TestLibraryForCustomer1()
      {
          string xmlFromMessage = @"&lt;Library&gt;
              &lt;Writer ID=""writer1""&gt;&lt;Name&gt;Shakespeare&lt;/Name&gt;&lt;/Writer&gt;
              &lt;Writer ID=""writer2""&gt;&lt;Name&gt;Tolkien&lt;/Name&gt;&lt;/Writer&gt;
              &lt;Book&gt;&lt;WriterRef REFID=""writer1"" /&gt;&lt;Title&gt;King Lear&lt;/Title&gt;&lt;/Book&gt;
              &lt;Book&gt;&lt;WriterRef REFID=""writer2"" /&gt;&lt;Title&gt;The Hobbit&lt;/Title&gt;&lt;/Book&gt;
              &lt;Book&gt;&lt;WriterRef REFID=""writer2"" /&gt;&lt;Title&gt;Lord of the Rings&lt;/Title&gt;&lt;/Book&gt;
               &lt;/Library&gt;"; 
  
          var titleXPathFromConfigurationFile = "./Title"; 
          var writerXPathFromConfigurationFile = "for $curr in . return /Library/Writer[@ID=$curr/WriterRef/@REFID]/Name";
  
          var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b =&gt; b.Key, b =&gt; b.Value);
  
          Assert.AreEqual("Shakespeare", library["King Lear"]);
          Assert.AreEqual("Tolkien", library["The Hobbit"]);
          Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
      }
  
      [TestMethod]
      public void TestLibraryForCustomer2()
      {
          string xmlFromMessage = @"&lt;Library&gt;
                  &lt;Writer ID=""writer1""&gt;
                      &lt;Name&gt;Shakespeare&lt;/Name&gt;
                      &lt;Book&gt;&lt;Title&gt;Sonnet 18&lt;/Title&gt;&lt;/Book&gt;
                  &lt;/Writer&gt;
                  &lt;Writer ID=""writer2""&gt;
                      &lt;Name&gt;Tolkien&lt;/Name&gt;
                      &lt;Book&gt;&lt;Title&gt;The Hobbit&lt;/Title&gt;&lt;/Book&gt;
                      &lt;Book&gt;&lt;Title&gt;Lord of the Rings&lt;/Title&gt;&lt;/Book&gt;
                  &lt;/Writer&gt;
              &lt;/Library&gt;";
  
          var titleXPathFromConfigurationFile = "./Title";
          var writerXPathFromConfigurationFile = "../Name";
  
          var library = ExtractBooks(xmlFromMessage, titleXPathFromConfigurationFile, writerXPathFromConfigurationFile).ToDictionary(b =&gt; b.Key, b =&gt; b.Value);
  
          Assert.AreEqual("Shakespeare", library["Sonnet 18"]);
          Assert.AreEqual("Tolkien", library["The Hobbit"]);
          Assert.AreEqual("Tolkien", library["Lord of the Rings"]);
      }
  
  
      public IEnumerable&lt;KeyValuePair&lt;string,string&gt;&gt; ExtractBooks(string xml, string titleXPath,  string writerXPath)
      {
          var library = XDocument.Parse(xml);
          foreach(var book in library.Descendants().Where(d =&gt; d.Name == "Book"))
          {
              var title = book.XPathSelectElement(titleXPath).Value;
              var writer = book.XPathSelectElement(writerXPath).Value;
              yield return new KeyValuePair&lt;string, string&gt;(title, writer);
          }
      }
  </code></pre>