Bug 20714 - Comments in XML fragments throw exceptions in DataSet.ReadXml
Summary: Comments in XML fragments throw exceptions in DataSet.ReadXml
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data ()
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-19 08:40 UTC by Matthew Leibowitz
Modified: 2017-09-06 17:05 UTC (History)
3 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 Matthew Leibowitz 2014-06-19 08:40:35 UTC
If there are any non-elements between the XML schema and the dataset data when reading an xml fragment, Mono throws a null ref exception.

Using this sample code:

const string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<!-- structure for dataset starts here -->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name='Root'>
    <xs:complexType>
      <xs:sequence>
        <xs:element name='Child' type='xs:string' />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
<!-- data for dataset starts here -->
<dataset>
  <Root>
    <Child>Row One!</Child>
  </Root>
</dataset>";
TextReader stringReader = new StringReader (xml);
XmlReaderSettings settings = new XmlReaderSettings {
  ConformanceLevel = ConformanceLevel.Fragment
};
XmlReader reader = XmlReader.Create (stringReader, settings);
DataSet ds = new DataSet ();
ds.ReadXml (reader, XmlReadMode.Fragment);

results in an exception:

Unhandled Exception:
System.ArgumentNullException: Argument cannot be null.
Parameter name: name
  at System.Xml.XmlConvert.VerifyName (System.String name) [0x0003d] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlConvert.cs:807
  at System.Xml.XmlElement..ctor (System.String prefix, System.String localName, System.String namespaceURI, System.Xml.XmlDocument doc, Boolean atomizedNames) [0x00029] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlElement.cs:79
  at System.Xml.XmlDocument.CreateElement (System.String prefix, System.String localName, System.String namespaceURI) [0x00000] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlDocument.cs:415
  at System.Data.DataSet.ReadXml (System.Xml.XmlReader reader, XmlReadMode mode) [0x0017b] in C:\cygwin\sources\mono\mcs\class\System.Data\System.Data\DataSet.cs:982
  at ConsoleApplication2.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

This exception is a result of the reader assuming that the next node after the schema is an element - which in the case of the sample code, it is a comment.
Comment 1 Matthew Leibowitz 2014-06-19 10:05:06 UTC
This is a direct bug in the reading of the xml dataset from an xml frag. 
I made a fix in my working branch 
https://github.com/mattleibow/mono/commit/f0b412d0facfcb6882848f792f5b4aa24e5177e7

The error is partially correct from System.Xml's side, this line correctly fails:
  doc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);

At this point, reader.Prefix, LocalName and Namespace are all empty strings (""). The real error in this case is the fact that we are trying to get the local name from a comment, which is invalid.


MS.NET actually throws this exception in this case, but this is a different bug:
  doc.CreateElement("", "", "");


.NET exception:
System.ArgumentException was unhandled
  _HResult=-2147024809
  _message=The local name for elements or attributes cannot be null or an empty string.
  HResult=-2147024809
  IsTransient=false
  Message=The local name for elements or attributes cannot be null or an empty string.
  Source=System.Xml
  StackTrace:
       at System.Xml.XmlElement..ctor(XmlName name, Boolean empty, XmlDocument doc)
       at System.Xml.XmlDocument.CreateElement(String prefix, String localName, String namespaceURI)
       at ConsoleApplication4.Program.Main(String[] args) in c:\Users\mattl_000.SURFACE\Documents\Visual Studio 2013\Projects\ConsoleApplication4\ConsoleApplication4\Program.cs:line 27
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


Mono exception:
Unhandled Exception:
System.ArgumentNullException: Argument cannot be null.
Parameter name: name
  at System.Xml.XmlConvert.VerifyName (System.String name) [0x0003d] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlConvert.cs:807
  at System.Xml.XmlElement..ctor (System.String prefix, System.String localName, System.String namespaceURI, System.Xml.XmlDocument doc, Boolean atomizedNames) [0x00029] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlElement.cs:79
  at System.Xml.XmlDocument.CreateElement (System.String prefix, System.String localName, System.String namespaceURI) [0x00000] in C:\cygwin\sources\mono\mcs\class\System.XML\System.Xml\XmlDocument.cs:415
  at ConsoleApplication2.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
Comment 2 Matthew Leibowitz 2014-06-19 16:25:10 UTC
NOTE: possibly fixed System.Data part in https://github.com/mattleibow/mono/commit/f0b412d0facfcb6882848f792f5b4aa24e5177e7 which is part of the https://github.com/mono/mono/pull/1087 pull request
Comment 3 Matthew Leibowitz 2015-08-25 00:25:59 UTC
This was fixed when the code was switched to use the MS reference source