Bug 16 - XNode.ReplaceWith() throws exception when provided null arguments.
Summary: XNode.ReplaceWith() throws exception when provided null arguments.
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: 2.10.x
Hardware: Macintosh Mac OS
: --- major
Target Milestone: Untriaged
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2011-07-18 14:13 UTC by Mason Bially
Modified: 2011-07-25 15:33 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Fix for original bug reported and allow replacing of XDocument root (4.25 KB, patch)
2011-07-25 15:00 UTC, Martin Potter
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Mason Bially 2011-07-18 14:13:52 UTC
When the array passed to XNode.ReplaceWith() contains null values, those values are expected to be ignored (according to both the documentation and Microsoft's implementation). Calling XNode.ReplaceWith() with a single null value (or multiple null values) is expected to have the same effect as XNode.Remove(). However, in Mono's implementation of XNode.ReplaceWith(), an exception is raised in either of these cases.

The C# shell session below illustrates this exception:

$ csharp2 -r:System.Xml.Linq
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> using System.Xml.Linq;
csharp> var doc = new XDocument(new XElement("foo", new XElement("bar")));
csharp> doc;
<foo>
  <bar />
</foo>
csharp>  

        doc.Elements().Elements().First();
<bar />
csharp> doc.Elements().Elements().First().ReplaceWith(null);
System.InvalidOperationException: Attempt to get string from null
  at System.Xml.Linq.XUtil.ToString (System.Object o) [0x00000] in <filename unknown>:0 
  at System.Xml.Linq.XUtil.ToNode (System.Object o) [0x00000] in <filename unknown>:0 
  at System.Xml.Linq.XNode.AddAfterSelf (System.Object content) [0x00000] in <filename unknown>:0 
  at System.Xml.Linq.XNode.AddAfterSelf (System.Object[] content) [0x00000] in <filename unknown>:0 
  at System.Xml.Linq.XNode.ReplaceWith (System.Object[] items) [0x00000] in <filename unknown>:0 
  at Class3.Host (System.Object& $retval) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <filename unknown>:0 
  at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <filename unknown>:0 

Expected Behavior:

In the C# shell run above, the element <bar /> should have been removed from the XDocument.

Actual Behavior:

An InvalidOperationException was thrown.
Comment 1 Martin Potter 2011-07-25 15:00:56 UTC
Created attachment 13 [details]
Fix for original bug reported and allow replacing of XDocument root
Comment 2 Martin Potter 2011-07-25 15:12:13 UTC
Comment on attachment 13 [details]
Fix for original bug reported and allow replacing of XDocument root

Changed ReplaceWith to first remove the XNode, then add the contents to the parent node per documentation on msdn (http://msdn.microsoft.com/en-us/library/bb302711.aspx). I changed references from Parent to Owner to allow calling ReplaceWith on child nodes of an XDocument, which is not currently possible due to Parent being null.
Comment 3 Atsushi Eno 2011-07-25 15:33:21 UTC
Applied the patch, thanks.