dotnet-script / dotnet-script

Run C# scripts from the .NET CLI.
MIT License
2.73k stars 167 forks source link

Cannot use XmlSerializer dotnet-script #696

Open Timo-Weike opened 1 year ago

Timo-Weike commented 1 year ago

If I try to execute code like

using System.Xml.Serialization;
using System.IO;

[XmlRoot("root")]
public class MyXml
{
    [XmlAttribute]
    public string Name { get; set; } = string.Empty;
}

var text = File.ReadAllText("file.xml");

using (var stream = new StringReader(text))
{
    var deserialized = new XmlSerializer(typeof(MyXml)).Deserialize(stream);

    var value = deserialized is not null
        ? (MyXml)deserialized
        : default;

    Console.WriteLine(value);
}

I get the error

System.ArgumentException: Identifier 'Submission#0' is not CLS-compliant. (Parameter 'ident')
   at System.Xml.Serialization.CodeIdentifier.CheckValidIdentifier(String ident)
   at System.Xml.Serialization.CodeIdentifier.EscapeKeywords(String identifier, StringBuilder sb)
   at System.Xml.Serialization.CodeIdentifier.GetCSharpName(Type t, Type[] parameters, Int32 index, StringBuilder sb)
   at System.Xml.Serialization.CodeIdentifier.GetCSharpName(Type t, Type[] parameters, Int32 index, StringBuilder sb)
   at System.Xml.Serialization.CodeIdentifier.GetCSharpName(Type t)
   at System.Xml.Serialization.TypeDesc.get_CSharpName()
   at System.Xml.Serialization.SourceInfo.CastTo(TypeDesc td)
   at System.Xml.Serialization.XmlSerializationWriterILGen.WriteElements(SourceInfo source, String enumSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, String arrayName, Boolean writeAccessors, Boolean isNullable)
   at System.Xml.Serialization.XmlSerializationWriterILGen.WriteMember(SourceInfo source, String choiceSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, TypeDesc memberTypeDesc, Boolean writeAccessors)
   at System.Xml.Serialization.XmlSerializationWriterILGen.GenerateTypeElement(XmlTypeMapping xmlTypeMapping)
   at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace)
   at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location)
   at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace, String location)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
   at Submission#0.<<Initialize>>d__0.MoveNext() in main.csx:line 15
--- End of stack trace from previous location ---
   at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in C:\Users\runneradmin\AppData\Local\Temp\tmpFE4C\Dotnet.Script.Core\ScriptRunner.cs:line 110
filipw commented 1 year ago

This is unfortunately a bug in the Roslyn compiler. The dynamic type names used are not compatible with the XmlSerializer. See https://github.com/dotnet/roslyn/issues/22859

DanJBower commented 1 year ago

I've just run into this issue. Is there a work around at all?