Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use XmlSerializer dotnet-script #696

Open
Timo-Weike opened this issue Dec 22, 2022 · 2 comments
Open

Cannot use XmlSerializer dotnet-script #696

Timo-Weike opened this issue Dec 22, 2022 · 2 comments

Comments

@Timo-Weike
Copy link

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
Copy link
Member

filipw commented Dec 23, 2022

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

@DanJBower
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants