views:

18114

answers:

10

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 passing that into the constructor:

XmlSerializer serializer = new XmlSerializer(typeof(DataClass));

I am getting an exception saying: There was an error reflecting type.

Inside the data class there is another composite object. Does this also need to have the [Serializable] attribute or by having it on the top object does it recursively apply it to all objects inside?

Any thoughts?

+44  A: 

Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.

You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore()] attribute.

I don't think that XmlSerializer uses the [Serializable] attribute, so I doubt that is the problem.

Lamar
A: 

Also note that you cannot serialize user interface controls and that any object you want to pass onto the clipboard must be serializable otherwise it cannot be passed across to other processes.

Phil Wright
+1  A: 

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

Gulzar
+1  A: 

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.

Darren
+13  A: 

Remember that serialized classes must have default (i.e. parameterless) constructors. If you have no constructor at all, that's fine; but if you have a constructor with a parameter, you'll need to add the default one too.

Jeremy McGee
Thanks for the reminder! I hate that this is a runtime error with little explanation.
Jared Updike
+3  A: 

Also be aware that XmlSerializer cannot serialize abstract properties.. See my question here (which I have added the solution code to)..

XML Serialization and Inherited Types

Rob Cooper
A: 

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

The domain classes are shared between client and server.

A: 

I've discovered that the Dictionary class in .Net 2.0 is not serializable using XML, but serializes well when binary serialization is used.

I found a work around here.

Charlie Salts
A: 

Can you describe DataClass? all its properties in detail. You may have used a generic type which is not supported.

chugh97
A: 

If you need to handle specific attributes (i.e. Dictionary, or any class), you can implement the IXmlSerialiable interface, which will allow you more freedom at the cost of more verbose coding.

public class NetService : IXmlSerializable
{
#region Data

public string Identifier = String.Empty;

public string Name = String.Empty;

public IPAddress Address = IPAddress.None;
public int Port = 7777;

#endregion

#region IXmlSerializable Implementation

public XmlSchema GetSchema() { return (null); }

public void ReadXml(XmlReader reader)
{
// Attributes
Identifier = reader[XML_IDENTIFIER];
if (Int32.TryParse(reader[XML_NETWORK_PORT], out Port) == false)
throw new XmlException("unable to parse the element " + typeof(NetService).Name + " (badly formatted parameter " + XML_NETWORK_PORT);
if (IPAddress.TryParse(reader[XML_NETWORK_ADDR], out Address) == false)
throw new XmlException("unable to parse the element " + typeof(NetService).Name + " (badly formatted parameter " + XML_NETWORK_ADDR);
}

public void WriteXml(XmlWriter writer)
{
    // Attributes
    writer.WriteAttributeString(XML_IDENTIFIER, Identifier);
    writer.WriteAttributeString(XML_NETWORK_ADDR, Address.ToString());
    writer.WriteAttributeString(XML_NETWORK_PORT, Port.ToString());
}

private const string XML_IDENTIFIER = "Id";

private const string XML_NETWORK_ADDR = "Address";

private const string XML_NETWORK_PORT = "Port";

#endregion
}

There is an intersting article, which show an elegant way to implements a sophisticated way to "extend" the XmlSerializer.


The article say:

IXmlSerializable is covered in the official documentation, but the documentation states it's not intended for public use and provides no information beyond that. This indicates that the development team wanted to reserve the right to modify, disable, or even completely remove this extensibility hook down the road. However, as long as you're willing to accept this uncertainty and deal with possible changes in the future, there's no reason whatsoever you can't take advantage of it.

Because this, I suggest to implement you're own IXmlSerializable classes, in order to avoid too much complicated implementations.

...it could be straightforward to implements our custom XmlSerializer class using Reflection.

Luca