I'm trying to learn some Linq to XML stuff, and I came across the XPathSelectElement function in XElement. This function seems to do just what I need, but for some reason, I can't use it! Check out my code:
XElement rootElement = XElement.Load(dataFile);
XElement parentElement = rootElement.XPathSelectElement(xPath);
I have included references to System.Xml.Linq everywhere that is needed. All the other stuff in that library that I have tried appears to be working, but XPathSelectElement doesn't even appear in the Intellisense in visual studio.
When building the above code, I get the following error:
Error 1 'System.Xml.Linq.XElement' does not contain a definition for 'XPathSelectElement' and no extension method 'XPathSelectElement' accepting a first argument of type 'System.Xml.Linq.XElement' could be found (are you missing a using directive or an assembly reference?) C:\PageHelpControl\PageHelp.cs 155 50 HelpControl
The methods you are trying to use are extension menthods. You need to include System.Xml.XPath namespace.
Just to tie the two answers together...
XPathSelectElement
is an extension method. To use it as an extension method (i.e. as if it were an instance method on XNode
) you need to have a using
directive in your source code for the relevant namespace:
using System.Xml.XPath;
(That's where the Extensions
class which contains the extension method lives.)
This works in the same way that you need using System.Linq;
in your code before you can use Select
, Where
etc on IEnumerable<T>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With