2

Possible Duplicate:
XmlSerializer - There was an error reflecting type

XmlSerializer ser = new XmlSerializer(typeof(configType));

There was an error reflecting type 'MyNameSpace.configType'.

I'll pare away at configType some more in the hope of finding the problem. Apart from removing ostensibly redundant attributes the only change I made to it after generating it from xsd.exe was replacing one of the generated classes with my own so I could better implement the serialization of optional elements.

Thanks for the hints to examine that innerException, it traced back to the class I made implement IXmlSerializable, which is now in its own file and presented below:

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace MyNameSpace
{
    [Serializable(), XmlType(Namespace = "MyNameSpace")] //Naughty xsd.exe!
    public class OpOpts : IXmlSerializable
    {
        void defaultFlags()
        {
            Opt1 = true;
            Opt2 = true;
            Opt5 = false;
        }

        public System.Xml.Schema.XmlSchema GetSchema() { return (null); }
        public void ReadXml(XmlReader reader)
        {
            defaultFlags();
            //            reader.Read();
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                switch (reader.Name)
                {
                    case "Opt1": Opt1 = reader.ReadElementContentAsBoolean();
                        break;
                    case "Opt2": Opt2 = reader.ReadElementContentAsBoolean();
                        break;
                    case "Opt5": Opt5 = reader.ReadElementContentAsBoolean();
                        break;
                    default:
                        throw new Exception("Invalid XML tag " + reader.Name + " found under OpOpts.");
                }
            }
        }
        enum OptionEnum
        {
            Opt1, Opt2, Opt5
        }
        public void WriteXml(XmlWriter writer)
        {
            OpOpts defOpts = new OpOpts();
            if (Opt1 != defOpts.Opt1) writer.WriteElementString("Opt1", Opt1.ToString());
            if (Opt2 != defOpts.Opt2) writer.WriteElementString("Opt2", Opt2.ToString());
            if (Opt5 != defOpts.Opt5) writer.WriteElementString("Opt5", Opt5.ToString());
        }

        public bool Opt1 { get; set; }
        public bool Opt2 { get; set; }
        public bool Opt5 { get; set; }
    }
}

jdehaan wrote a nice article, Proper way to implement IXmlSerializable?. After reading his codeproject I found that the stuff that xsd.exe generated for my proto-class was breaking the class I extended to implement IXMLSerializable.

Specifically the attribute [XmlType(Namespace = "MyNameSpace")] causes the error "reflecting property". When I remove it I get a bit further, the containing class passes deserialization of everything else but cries "Invalid XML tag" when it comes to the member representing my class above.

Community
  • 1
  • 1
John
  • 6,433
  • 7
  • 47
  • 82
  • 2
    Look at the inner exception, it should give you more information – Kyle W Sep 15 '11 at 00:20
  • 1
    Look at the inner exception, it should give you more information about what's wrong. The most common issue I see is that the type being (de)serialized is not public. – Ryan Stecker Sep 15 '11 at 00:21

0 Answers0