I have an XML file from which I get an XElement element, then I try to create element of type R_Element that inherits from XElement, but when I do that, I can’t get all the ancestors of it.
public class R_Element : XElement
{
public readonly XElement x_element;
public R_Element(XElement elem) : base(elem)
{
x_element = elem;
return;
}
}
I try the following to test
XElement node_1 = new XElement("node_1");
XElement node_2 = new XElement("node_2");
XElement node_3 = new XElement("node_3");
XElement root = new XElement("root",
new XElement("root_1",
node_1,
node_2),
node_3);
R_Element r_node;
r_node = new R_Element(node_2);
Console.WriteLine(r_node.Ancestors().Count());
The resulting ancestor number is 0.
Is there a way to convert the XElement element to another element of type R_Element while preserving the object?
I tried to use it through the x_element member, but it happens that at some point it loses connection with its ancestors except for the first one, like when I pass an XElement element as an argument in some function. I couldn’t reproduce it in a test application.