Description
I'm getting an exception when I call XmlSerializer.Deserialize - 'Element' is an invalid XmlNodeType.
It seems unlikely that there is a bug in such battle-tested code as the XmlSerializer, however, I have tried a couple of online XML validators and they don't complain.
I am trying to deserialize
<Response>
<RT.VAL.ID>123</RT.VAL.ID>
<Error>Parcel id invalid format [<RT.VAL.PARCEL.NO/>] should be 4N/N.</Error>
</Response>
Reproduction Steps
using System.Xml.Serialization;
namespace Tests;
public class XmlHelperTests
{
[XmlRoot("Response")]
public class UpdateValuationResponse
{
[XmlElement("RT.VAL.ID")]
public string? ValidationId { get; set; }
[XmlElement("Error")]
public string? Error { get; set; }
}
[Fact]
public void CanXmlDeserializeErrorResponse()
{
// Arrange
var xml = "<Response><RT.VAL.ID>123</RT.VAL.ID><Error>Parcel id invalid format [<RT.VAL.PARCEL.NO/>] should be 4N/N.</Error></Response>";
// Act
var serializer = new XmlSerializer(typeof(UpdateValuationResponse));
using var reader = new StringReader(xml);
var result = serializer.Deserialize(reader) as UpdateValuationResponse; // throws InvalidOperationException
// Assert
Assert.StartsWith("Parcel id invalid format", result!.Error);
}
}
Throws an InvalidOperationException - There is an error in XML document (1, 104).
Expected behavior
The object should be deserialized and the Error should contain "Parcel id invalid format [<RT.VAL.PARCEL.NO/>] should be 4N/N."
Actual behavior
System.InvalidOperationException: There is an error in XML document (1, 104).
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at Tests.XmlHelperTests.CanXmlDeserializeErrorResponse() in C:\code\tcc\Tcc.Ozone.Client\Tcc.Ozone.Client.Tests\Tests.cs:line 27
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
System.Xml.XmlException: 'Element' is an invalid XmlNodeType.
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8
Other information
No response
Description
I'm getting an exception when I call XmlSerializer.Deserialize - 'Element' is an invalid XmlNodeType.
It seems unlikely that there is a bug in such battle-tested code as the XmlSerializer, however, I have tried a couple of online XML validators and they don't complain.
I am trying to deserialize
Reproduction Steps
Throws an InvalidOperationException - There is an error in XML document (1, 104).
Expected behavior
The object should be deserialized and the Error should contain "Parcel id invalid format [<RT.VAL.PARCEL.NO/>] should be 4N/N."
Actual behavior
System.InvalidOperationException: There is an error in XML document (1, 104).
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at Tests.XmlHelperTests.CanXmlDeserializeErrorResponse() in C:\code\tcc\Tcc.Ozone.Client\Tcc.Ozone.Client.Tests\Tests.cs:line 27
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
System.Xml.XmlException: 'Element' is an invalid XmlNodeType.
at System.Xml.XmlReader.FinishReadElementContentAsXxx()
at System.Xml.XmlReader.ReadElementContentAsString()
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8
Other information
No response