so I'm doing some xml manipulation and I've found myself in a strange situation. After adding an XElement with null Parent to another XElement I still had the Parent set to null. So after some testing I've fond the problem, but I don't clearly understand the behaviour.
XDocument x=new XDocument(new XElement("asd"));
XElement root=x.Root;
XElement parent=new XElement("parent");
Console.WriteLine(root.Parent?.Name??"null"); //still null
parent.Add(root);
Console.WriteLine(root.Parent?.Name??"null"); //should be
root.Remove(); // should throw InvalidOperationException
parent.Add(root);
Console.WriteLine(root.Parent?.Name??"null");//parent
It seems that when you add an XElement that is the Root of a XDocument the XElement is copied and you need to call Remove before adding. The documentation says that Remove should throw exception when Parent is null, but instead in this case it seems that it is removing the relation between the XDocument and its root. Is this interpration correct or is there another explanation?