9

So, I have an XElement, that contains multiple child elements. I can successfully declare the XElement, and write it to a file:

Test.project:

<?xml version="1.0" encoding="utf-8"?>
<project>
    <child>
        <grand-child1>
            <great-grand-child1>Hello There!</great-grand-child1>
            <great-grand-child2>Hello World!</great-grand-child2>
        </grand-child1>
        <grand-child2>Testing 123...</grand-child2>
    </child>
</project>

Then I'm trying to read from the file. I searched for ways to get child and grand-child nodes, and found I can use XElement.XPathSelectElement(). The problem is, Visual C# doesn't recognize XPathSelectElement as a method for an XElement. I've searched for usage examples for the method, and they all say to use XElement.XPathSelectElement.

For example, I tried:

x_el = new XElement("project",
    new XElement("child",
        new XElement("grand-child", "Hello World!")
);

string get_string = x_el.XPathSelectElement("child/grand-child");

...but XPathSelectElement is not recognized. What am I doing wrong?

Dominoed
  • 123
  • 3
  • 15
  • 1
    have you add reference to `System.Xml.Linq`? – Damith Aug 22 '13 at 03:27
  • 1
    @JuStDaN I have written code. I'll edit my post. – Dominoed Aug 22 '13 at 03:31
  • @Damith Yes, this is in the same class as where I create the XElement and save it to a file. – Dominoed Aug 22 '13 at 03:31
  • A very simple search of `[C#] XDocument XPath` yields [This Answer](http://stackoverflow.com/a/6209890/1484320). – Ichabod Clay Aug 22 '13 at 03:41
  • 1
    What do you mean by "not recognized"? What's the compiler error message? – Arman Bimatov Aug 22 '13 at 03:41
  • 2
    you need to add `System.Xml.XPath` namespace as well – Damith Aug 22 '13 at 03:46
  • I pasted the error under Damith's answer. – Dominoed Aug 22 '13 at 03:49
  • @Dominoed is that fixed your issue? – Damith Aug 22 '13 at 03:52
  • @Damith hmm...if I use `string get_String = this.x.XPathSelectElement("child/grand-child").ToString();` then it returns an error when I run it: Object reference not set to an instance of an object. – Dominoed Aug 22 '13 at 03:59
  • @Dominoed That's because `this.x` doesn't appear to be assigned anywhere in your question. That is a separate issue from the one you brought up in your question originally. – Asad Saeeduddin Aug 22 '13 at 05:27
  • @Asad `this.x` is defined earlier in the same class as an XElement, and it is recognized as an XElement in Visual Studio when I type `this.x.XPathSelecctElement()`. What else could be the problem? – Dominoed Aug 22 '13 at 13:26
  • @Asad Do you know what I'm doing wrong..? – Dominoed Aug 22 '13 at 16:53
  • @Dominoed Again, this is a separate issue from the one your question was originally about. `this.x` is null, which is to say it is not assigned a value (assigned means a different thing from defined). You should ask a separate question, including the code where you define and and assign `x`. – Asad Saeeduddin Aug 22 '13 at 17:02

1 Answers1

5

You need to add System.Xml.XPath namespace as well like

using System.Xml.XPath;

after that try below

x_el = new XElement("project",
    new XElement("child",
        new XElement("grand-child", "Hello World!")
));
// XPathSelectElement method return XElement not string , use var or XElement 
XElement element = x_el.XPathSelectElement("child/grand-child");
string get_string = element.ToString()

Or

var get_string = x_el.XPathSelectElement("child/grand-child").ToString();
Damith
  • 62,401
  • 13
  • 102
  • 153
  • The Error: '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?) – Dominoed Aug 22 '13 at 03:43