EDIT: I found the error. I was setting the elements in the wrong place, and then calling their XPath in the right (different) place.
I'm trying to get values of XML elements inside of another XElement, and when I try to get the value using XPath
, it gives me the error in the title above. All of this is in the same class:
XElement x;
this.x =
new XElement("parent",
new XElement("child",
new XElement("grand-child1",
new XElement("great-grand-child1","Hello"),
new XElement("great-grand-child2","World!")
)
new XElement("grand-child2","Testing123")
)
);
string get_str = this.x.XPathSelectElement("child/grand-child1/great-grand-child1").ToString();
It gives no errors during coding, but when I run it, it gives me the error "Object reference not set to an instance of an object", and it highlights the string get_Str...
line.