Consider the following snippet
using System; using System.Xml.Linq; class Program { static void Main() { var family = XDocument.Parse("<p><c><g/></c></p>"); var usurper = family.Root.Descendants("g"); family.Root.Element("c").Remove(); family.Root.Add(usurper); Console.WriteLine(family.ToString()); Console.ReadKey(); } }
The output I get is @"<p />"
, but I would like @"<p><g/></p>"
. I can understand why this is happening, and there is justice, but this is not the result that I need.
If you change the order of the lines to
var usurper = family.Root.Descendants("g"); family.Root.Add(usurper); family.Root.Element("c").Remove();
a circular connection is established, and as a result of removing the event OutOfMemoryException
Is there a simple, practical way to make this work?
EDIT
The fix that I really performed was
var usurper = family.Root.Descendants("g").Single(); family.Root.Element("c").Remove(); family.Root.Add(usurper);
Sorry my redness.
source share