Fix bug #2146 - create snapshot copy of nodes before removal in Remove<T>().
authorAtsushi Eno <atsushi@ximian.com>
Thu, 19 Jan 2012 10:51:44 +0000 (19:51 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Thu, 19 Jan 2012 10:51:44 +0000 (19:51 +0900)
mcs/class/System.Xml.Linq/System.Xml.Linq/Extensions.cs
mcs/class/System.Xml.Linq/Test/System.Xml.XPath/ExtensionsTest.cs

index d13d0c8d88eb2296efd2fbd863478b81325a1af1..bf88996b0d8cdbd8c20c4473041d7470661cbe73 100644 (file)
@@ -168,7 +168,8 @@ namespace System.Xml.Linq
 
                public static void Remove<T> (this IEnumerable<T> source) where T : XNode
                {
-                       foreach (T item in source)
+                       var l = new List<T> (source);
+                       foreach (T item in l)
                                item.Remove ();
                }
        }
index 93d64290bedb10fbbbc6c6656947330678eb441d..a3c07501abd97d50a79f024f2285dd3f6f8c262e 100644 (file)
@@ -462,5 +462,20 @@ namespace MonoTests.System.Xml
                        IEnumerable att = (IEnumerable) d.XPathEvaluate ("/root/@a");
                        att.Cast<XAttribute> ().FirstOrDefault ();
                }
+
+               [Test] // bug #2146
+               public void RemoveDoesSnapshotCopy ()
+               {
+                       var xml = XElement.Parse ("<p><n>one</n><n>two</n><n>three</n></p>");
+                       xml.Elements ().Last ().NodesBeforeSelf ().Remove ();
+               }
+
+               [Test] // bug #2146
+               public void RemoveDoesSnapshotCopy2 ()
+               {
+                       var xml = XElement.Parse ("<p><n>one</n><n>two</n><n>three</n></p>");
+                       xml.Elements ().First ().NodesAfterSelf ().Remove ();
+                       Assert.IsTrue (!xml.ToString ().Contains ("three"), "#1");
+               }
        }
 }
\ No newline at end of file