home: Deserializer Can't find a Class Program-ClassA (Should be Program.ClassA)

Running the below code results in the following error “ExtendedXmlSerializer.Core.Sprache.ParseException: ‘An attempt was made to parse the identity ‘clr-namespace:;assembly=StackTypeSerialization:Program-ClassA’, but no type could be located with that name.’”. using release 2.1.15 targeting .Net Core 2.1

using ExtendedXmlSerializer.Configuration;
using System.Collections.Generic;
using System.Xml;

public class Program
{
    public static void Main()
    {
        var outList = new List<ClassA>();

        var classB = new ClassB();
        var classC = new ClassC();

        outList.Add(new ClassA()
        {
            blah = "Test1",
            InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah1", TestConcretePropertyB = "Blah1" },
            InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah2", TestConcretePropertyC = "Blah2" }
        });
        outList.Add(new ClassA()
        {
            blah = "Test2",
            InterfaceConcreteTypeA = new ClassB(),
            InterfaceConcreteTypeB = new ClassC()
        });
        outList.Add(new ClassA()
        {
            blah = "Test3",
            InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah3", TestConcretePropertyB = "Blah3" },
            InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah3", TestConcretePropertyC = "Blah3" }
        });


        var serializer = new ConfigurationContainer().Create();

        XmlWriterSettings settings = new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "\t"
        };

        using (XmlWriter writer = XmlWriter.Create("Test.xml", settings))
        {
            serializer.Serialize(writer, outList.ToArray());
        }

        List<ClassA> inList = new List<ClassA>();

        using (XmlReader reader = XmlReader.Create("Test.xml", new XmlReaderSettings { IgnoreWhitespace = false }))
        {
            inList = (List<ClassA>)serializer.Deserialize(reader);
        }

    }

    
    public class ClassA
    {        
        public string blah { get; set; }
        public InterfaceA InterfaceConcreteTypeA { get; set; }
        public InterfaceA InterfaceConcreteTypeB { get; set; }
    }

    public class ClassB : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyB { get; set; }
    }
    public class ClassC : InterfaceA
    {
        public string TestInterfaceProperty { get ; set; }
        public string TestConcretePropertyC { get; set; }
    }

    public interface InterfaceA
    {
        string TestInterfaceProperty { get; set; }
    }

}

ctrls-sb-ben 2019-0130 11-35-05

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

So I tried the same thing with .net 4.5. Same result.

Then I tried moving all of the classes to their own file. GUESS WHAT!!! it worked… this does not make ANY SENSE. It should not matter what file they are in.

Then I thought, wait a minute there is no enclosing namespace {} in the main program.cs file… So I added one and put the classes back in that file and it worked…

qwbz74a

for posterity, this is complete working code

I am not sure what you mean by “secretly failing without telling me”, it was definitely throwing an error on deserialize.


using ExtendedXmlSerializer.Configuration;
using System.Collections.Generic;
using System.Xml;
using ExtendedXmlSerializer.ExtensionModel.Xml;

namespace QuestionTypeSerialization
{

    public class Program
    {
        public static void Main()
        {

            var outList = new List<ClassA>();

            var classB = new ClassB();
            var classC = new ClassC();

            outList.Add(new ClassA()
            {
                blah = "Test1",
                InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah1", TestConcretePropertyB = "Blah1" },
                InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah2", TestConcretePropertyC = "Blah2" }
            });
            outList.Add(new ClassA()
            {
                blah = "Test2",
                InterfaceConcreteTypeA = new ClassB(),
                InterfaceConcreteTypeB = new ClassC()
            });
            outList.Add(new ClassA()
            {
                blah = "Test3",
                InterfaceConcreteTypeA = new ClassB() { TestInterfaceProperty = "Blah3", TestConcretePropertyB = "Blah3" },
                InterfaceConcreteTypeB = new ClassC() { TestInterfaceProperty = "Blah3", TestConcretePropertyC = "Blah3" }
            });


            var serializer = new ConfigurationContainer().Create();

            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent = true,
                IndentChars = "\t"
            };

            using (var xmlWriter = XmlWriter.Create("test.xml", settings))
            {
                serializer.Serialize(xmlWriter, outList);
            }


            var readerSettings = new XmlReaderSettings { IgnoreWhitespace = false };

            using (var xmlreader = XmlReader.Create("test.xml", readerSettings))
            {
                var blah = (List<ClassA>)serializer.Deserialize(xmlreader);
            }

        }
    }

    public class ClassA
    {
        public string blah { get; set; }
        public InterfaceA InterfaceConcreteTypeA { get; set; }
        public InterfaceA InterfaceConcreteTypeB { get; set; }
    }


    public class ClassB : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyB { get; set; }
    }
    public class ClassC : InterfaceA
    {
        public string TestInterfaceProperty { get; set; }
        public string TestConcretePropertyC { get; set; }
    }

    public interface InterfaceA
    {
        string TestInterfaceProperty { get; set; }
    }

}

Yep it all works now even with the reader and writer.

Thank you @Mike-EEE, I know it is not your problem but this is holding me up from moving along with my project. Any help would be very appreciated.

Also I have noted the mismatch between the array and the list, I removed the ToArray() in my test code. Same error message but we already new that.