This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 16 - XNode.ReplaceWith() throws exception when provided null arguments.
: XNode.ReplaceWith() throws exception when provided null arguments.
Status: RESOLVED FIXED
Product: Class Libraries
Classification: Mono
Component: Sys.XML
: 2.10.x
: Macintosh Mac OS
: --- major
: ---
Assigned To: Atsushi Eno
:
:
:
:
  Show dependency treegraph
 
Reported: 2011-07-18 14:13 EDT by Mason Bially
Modified: 2011-07-25 15:33 EDT (History)
4 users (show)

See Also:
Tags:
Test Case URL:
External Submit: ---


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

Description Mason Bially 2011-07-18 14:13:52 EDT
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 EDT
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 EDT
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 EDT
Applied the patch, thanks.

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