Bug 19447 - Microsoft Xml Document Transform Remove fails with NullReferenceException
Summary: Microsoft Xml Document Transform Remove fails with NullReferenceException
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.XML ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-05-01 06:16 UTC by Matt Ward
Modified: 2016-05-19 15:22 UTC (History)
2 users (show)

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

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 Matt Ward 2014-05-01 06:16:45 UTC
Created attachment 6692 [details]
Unit test for XDT remove transform.

Using Microsoft's Xml Document Transform library with a transform that removes an element fails when running on Mono with a NullReferenceException

Tested with Mono 3.4.0 - MonoFramework-MDK-3.4.0.202.macos10.xamarin.x86.pkg
Microsoft.Web.Xdt library v1.0.0.0 from NuGet.

Test class is attached. Full project code is available on github:
https://github.com/mrward/xdt-mono-tests



Exception thrown:

Microsoft.Web.XmlTransform.XmlNodeException: Object reference not set to an instance of an object ---> System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.Web.XmlTransform.Remove.RemoveNode () [0x00000] in <filename unknown>:0 
  at Microsoft.Web.XmlTransform.Remove.Apply () [0x00000] in <filename unknown>:0 
  at Microsoft.Web.XmlTransform.Transform.ApplyOnce () [0x00000] in <filename unknown>:0 
  at Microsoft.Web.XmlTransform.Transform.Execute (Microsoft.Web.XmlTransform.XmlElementContext context, System.String argumentString) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Microsoft.Web.XmlTransform.XmlTransformationLogger.LogErrorFromException (System.Exception ex) [0x00000] in <filename unknown>:0 
  at Microsoft.Web.XmlTransform.Transform.Execute (Microsoft.Web.XmlTransform.XmlElementContext context, System.String argumentString) [0x00000] in <filename unknown>:0 
  at Microsoft.Web.XmlTransform.XmlTransformation.HandleElement (Microsoft.Web.XmlTransform.XmlElementContext context) [0x00000] in <filename unknown>:0 


The actual line that throws a null exception in XDT is:

https://github.com/mrward/xdt/blob/master/XmlTransform/XmlTransforms.cs#L57

The TargetNode is null after it is removed from its parent. The TargetNode is taken from an XmlNodeList:

https://github.com/mrward/xdt/blob/master/XmlTransform/XmlTransform.cs#L99
https://github.com/mrward/xdt/blob/master/XmlTransform/XmlElementContext.cs#L243

Debugging the XDT source code it looks like there is different behaviour for the XmlNodeList on Mono. Selecting an XmlNodeList from an XmlDocument, removing the first child from the XmlDocument and then looping over the XmlNodeList to get the first node again returns a null node on Mono. Microsoft's .NET Framework returns a non-null node which matches the child node removed. The test below works with Microsoft's .NET Framework 4.0 but fails on Mono 3.4.0.

[Test]
public void RemoveNodeFromRootAfterSingleIterationOfForEachLoopShouldReturnNotNullNode ()
{
	string xml = 
@"<root>
	<child id='1'/>
</root>";
	var doc = new XmlDocument ();
	doc.LoadXml (xml);
	XmlNodeList children = doc.SelectNodes ("//child");
	XmlNode firstChild = null;

	foreach (XmlNode node in children) {
		firstChild = node;
		break;
	}

	doc.DocumentElement.RemoveChild (firstChild);

	XmlNode firstChildAfterRemove = null;
	foreach (XmlNode node in children) {
		firstChildAfterRemove = node;
		break;
	}

	Assert.IsNotNull (firstChildAfterRemove);
	Assert.AreSame (firstChild, firstChildAfterRemove);
}
Comment 1 Matt Ward 2016-05-19 15:22:30 UTC
This seems to be fixed in Mono 4.4.