Skip to content

XmlSerializer deserialize xml with parameterless struct constructors which implement IXmlSerializable will throw an exception #108660

Open
@MauNguyenVan

Description

@MauNguyenVan

Description

XmlSerializer deserialize xml with parameterless struct constructors which implement IXmlSerializable will throw an exception

  • The Exception:
    System.InvalidOperationException: 'There is an error in XML document (0, 0).'
    Inner Exception
    InvalidProgramException: Common Language Runtime detected an invalid program.

Note that. XmlSerializer.Serialize() is working well, but XmlSerializer.Deserialize() is not

image

Reproduction Steps

Try it at Sharplab.io

using System;
using System.Xml;
using System.IO;
using System.Xml.Schema;
using System.Xml.Serialization;
try
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(StructPatamerterless));

    StructPatamerterless structPatamerterless = new StructPatamerterless();

    using TextWriter textWriter = new StringWriter();
    xmlSerializer.Serialize(textWriter, structPatamerterless);
    string xmlString = textWriter.ToString(); //xmlString: <?xml version="1.0" encoding="utf-16"?>< StructPatamerterless > 1 </ StructPatamerterless >

    using TextReader textReader = new StringReader(xmlString);
    var recreateObject = (StructPatamerterless)xmlSerializer.Deserialize(textReader); // Error here:"There is an error in XML document (0, 0)."
}
catch (Exception ex)
{
    //Error here:"There is an error in XML document (0, 0)."
    //InvalidProgramException: Common Language Runtime detected an invalid program.
    Console.WriteLine(ex.Message);
}


public struct StructPatamerterless : IXmlSerializable // the same error with record struct
{
    static int currentID;
    public int Value { get; private set; }
    public StructPatamerterless()
    {
        Value = currentID++;
    }

    public XmlSchema? GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        Value = reader.ReadElementContentAsInt();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteValue(Value);
    }
}

Expected behavior

The Xml content to deserialise correctly into a POCO class, without exception, in the same way as would happen if there wasn't a parameterless constructor defined.

Actual behavior

System.InvalidOperationException
HResult=0x80131509
Message=There is an error in XML document (0, 0).
Source=System.Private.Xml
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at ConsoleApp.Classes.XmlSerializerWithStructParameterless.TestWith_StructPatamerterless_IXmlSerializable() in E:\Development\Console\ConsoleApp\src\ConsoleApp\Classes\XmlSerializerWithStructParameterless.cs:line 27

This exception was originally thrown at this call stack:
[External Code]

Inner Exception 1:
InvalidProgramException: Common Language Runtime detected an invalid program.

Regression?

No response

Known Workarounds

.Net 8.0,
Windows 10 Pro -22H2 64 bit
VisualStudio 2022 Enterprise Version 17.11.4

Configuration

run on Console app

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions