I came across an issue in .NET Core 2.1 when validating an XML containing self-closing tag.
I have a model with property Value of generic type T:
[XmlInclude(typeof(Parameter<string>))]
public class Parameter
{
[XmlAttribute]
public string Name { get; set; }
}
public class Parameter<T> : Parameter
{
public T Value { get; set; }
}
The corresponding XSD Schema snippet:
<xs:complexType name="parameter">
<xs:attribute type="xs:string" name="Name" use="required" />
</xs:complexType>
<xs:complexType name="stringParameter">
<xs:complexContent>
<xs:extension base="parameter">
<xs:sequence>
<xs:element name="Value" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
Both the model and schema are part of a project targeting .NET Standard 2.0.
Then I created test project targeting .NET Core 2.1 and .NET Framework 4.6.1 where I am trying to validate (and deserialize) XML containing following lines:
<Parameters>
<Parameter xsi:type="stringParameter" Name="SomeName">
<Value />
</Parameter>
</Parameters>
.NET Framework 4.6.1 does not have any issue (XML is evaluated as valid and is sucessfully deserialized). Validation under .NET Core 2.1 fails (by which I do NOT mean that as a result the XML is invalid, the validation process just crashes) and throws the following exception:
System.InvalidOperationException: There is an error in XML document (1, 226). ---> System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.Xml.Schema.XmlUntypedConverter.ToString(Object value, IXmlNamespaceResolver nsResolver)
at System.Xml.Schema.XmlBaseConverter.ToString(Object value)
at System.Xml.XsdValidatingReader.ReadElementContentAsString()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRootClass.Read2_ParameterOfString(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRootClass.Read3_Parameter(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRootClass.Read4_RootClass(Boolean isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderRootClass.Read5_RootClass()
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
I could find two workarounds to make it work:
1.) In XML, replace <Value /> with <Value></Value>
or 2.) In XSD, add type="xs:string" to element Value
But still, it is strange, isn't it?
Small example with both schemas:
Serializer.zip
Operating system: Windows 10 1803
IDE: Visual Studio 2017 15.9.6
Thanks,
Natalia
I came across an issue in .NET Core 2.1 when validating an XML containing self-closing tag.
I have a model with property Value of generic type T:
The corresponding XSD Schema snippet:
Both the model and schema are part of a project targeting .NET Standard 2.0.
Then I created test project targeting .NET Core 2.1 and .NET Framework 4.6.1 where I am trying to validate (and deserialize) XML containing following lines:
.NET Framework 4.6.1 does not have any issue (XML is evaluated as valid and is sucessfully deserialized). Validation under .NET Core 2.1 fails (by which I do NOT mean that as a result the XML is invalid, the validation process just crashes) and throws the following exception:
I could find two workarounds to make it work:
1.) In XML, replace
<Value />with<Value></Value>or 2.) In XSD, add
type="xs:string"to element ValueBut still, it is strange, isn't it?
Small example with both schemas:
Serializer.zip
Operating system: Windows 10 1803
IDE: Visual Studio 2017 15.9.6
Thanks,
Natalia