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 (show other bugs)
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)

See Also:
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

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.

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

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.

Note You need to log in before you can comment on or make changes to this bug.