XmlSerializer won't serialize IEnumerable

后端 未结 7 936
滥情空心
滥情空心 2020-12-10 00:45

I have an invocation logger that is intended to record all method calls along with the parameters associated with the method using XmlSerializer. It works well for most of t

相关标签:
7条回答
  • 2020-12-10 01:06

    Basically an XmlSerializer can't serialize an interface. The solution, then, is to give it a concrete instance to serialize. Depending on how your invocation logger works, I would consider using

    var serializer = new XmlSerializer(value.GetType());
    
    0 讨论(0)
  • 2020-12-10 01:08

    The way you serialize an IEnumerable property is with a surrogate property

    [XmlRoot]
    public class Entity {
       [XmlIgnore]
       public IEnumerable<Foo> Foo { get; set; }
    
       [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
       public List<Foo> FooSurrogate { get { return Foo.ToList(); } set { Foo = value; } }
    }
    

    It's ugly, but it gets the job done. The nicer solution is to write a surrogate class (i.e. EntitySurrogate).

    0 讨论(0)
  • 2020-12-10 01:11

    To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. {your class} does not implement Add(System.Object).

    implement the Add() function, you might solve the problem

    0 讨论(0)
  • 2020-12-10 01:15

    I don't think you'll be able to serialize that. Try converting the IEnumerable to a List and then you will be able to serialize.

    0 讨论(0)
  • 2020-12-10 01:18

    XmlSerializer does not support this. Try YAXLib for these kinds serializations.

    0 讨论(0)
  • 2020-12-10 01:19

    Maybe not the best way, but it worked for me. I created a method that makes serialization.

    Use

    var xml = Util.ObjetoToXML(obj, null, null).OuterXml;

    method

            public static XmlDocument ObjetoToXML(object obj, XmlDocument xmlDocument, XmlNode rootNode)
        {
    
            if (xmlDocument == null)
                xmlDocument = new XmlDocument();
    
            if (obj == null) return xmlDocument;
    
            Type type = obj.GetType();
    
            if (rootNode == null) { 
                rootNode = xmlDocument.CreateElement(string.Empty, type.Name, string.Empty);
                xmlDocument.AppendChild(rootNode);
            }
    
            if (type.IsPrimitive || type == typeof(Decimal) || type == typeof(String) || type == typeof(DateTime))
            {
    
                // Simples types
                if (obj != null)
                    rootNode.InnerText = obj.ToString();
    
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
            {
                // Genericis types
    
                XmlNode node = null;
    
                foreach (var item in (IEnumerable)obj)
                {
                    if (node == null)
                    {
                        node = xmlDocument.CreateElement(string.Empty, item.GetType().Name, string.Empty);
                        node = rootNode.AppendChild(node);
                    }
    
    
                    ObjetoToXML(item, xmlDocument, node);
                }
    
            }
            else
            {
    
                // Classes types
                foreach (var propertie in obj.GetType().GetProperties())
                {
    
                    XmlNode node = xmlDocument.CreateElement(string.Empty, propertie.Name, string.Empty);
                    node = rootNode.AppendChild(node);
                    var valor = propertie.GetValue(obj, null);
    
                    ObjetoToXML(valor, xmlDocument, node);
                }
    
            }
    
    
            return xmlDocument;
    
        }
    
    0 讨论(0)
提交回复
热议问题