XmlSerializer - There was an error reflecting type

后端 未结 18 1530
借酒劲吻你
借酒劲吻你 2020-11-28 02:46

Using C# .NET 2.0, I have a composite data class that does have the [Serializable] attribute on it. I am creating an XMLSerializer class and passi

相关标签:
18条回答
  • 2020-11-28 03:31

    I have been using the NetDataSerialiser class to serialise my domain classes. NetDataContractSerializer Class.

    The domain classes are shared between client and server.

    0 讨论(0)
  • 2020-11-28 03:32

    I recently got this in a web reference partial class when adding a new property. The auto generated class was adding the following attributes.

        [System.Xml.Serialization.XmlElementAttribute(Order = XX)]
    

    I needed to add a similar attribute with an order one higher than the last in the auto generated sequence and this fixed it for me.

    0 讨论(0)
  • 2020-11-28 03:33

    All the objects in the serialization graph have to be serializable.

    Since XMLSerializer is a blackbox, check these links if you want to debug further into the serialization process..

    Changing where XmlSerializer Outputs Temporary Assemblies

    HOW TO: Debug into a .NET XmlSerializer Generated Assembly

    0 讨论(0)
  • 2020-11-28 03:35

    I too thought that the Serializable attribute had to be on the object but unless I'm being a complete noob (I am in the middle of a late night coding session) the following works from the SnippetCompiler:

    using System;
    using System.IO;
    using System.Xml;
    using System.Collections.Generic;
    using System.Xml.Serialization;
    
    public class Inner
    {
        private string _AnotherStringProperty;
        public string AnotherStringProperty 
        { 
          get { return _AnotherStringProperty; } 
          set { _AnotherStringProperty = value; } 
        }
    }
    
    public class DataClass
    {
        private string _StringProperty;
        public string StringProperty 
        { 
           get { return _StringProperty; } 
           set{ _StringProperty = value; } 
        }
    
        private Inner _InnerObject;
        public Inner InnerObject 
        { 
           get { return _InnerObject; } 
           set { _InnerObject = value; } 
        }
    }
    
    public class MyClass
    {
    
        public static void Main()
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(DataClass));
                TextWriter writer = new StreamWriter(@"c:\tmp\dataClass.xml");
                DataClass clazz = new DataClass();
                Inner inner = new Inner();
                inner.AnotherStringProperty = "Foo2";
                clazz.InnerObject = inner;
                clazz.StringProperty = "foo";
                serializer.Serialize(writer, clazz);
            }
            finally
            {
                Console.Write("Press any key to continue...");
                Console.ReadKey();
            }
        }
    
    }
    

    I would imagine that the XmlSerializer is using reflection over the public properties.

    0 讨论(0)
  • 2020-11-28 03:36

    I was getting the same error when I created a property having a datatype - Type. On this, I was getting an error - There was an error reflecting type. I kept checking the 'InnerException' of every exception from the debug dock and got the specific field name (which was Type) in my case. The solution is as follows:

        [XmlIgnore]
        public Type Type { get; set; }
    
    0 讨论(0)
  • 2020-11-28 03:38

    Most common reasons by me:

     - the object being serialized has no parameterless constructor
     - the object contains Dictionary
     - the object has some public Interface members
    
    0 讨论(0)
提交回复
热议问题