XML Serialization

S

Swappy

hi,

I m trying to serialize the object of the serializable class DsExportDatabase.
But
XmlSerializer serializer = new XmlSerializer(typeof(DsExportDatabase));

This line throws me exception that "There was an error reflecting type "
wherein i have made [Serializable] to the class DsExportDatabase.


Can anybody tell me why this error occure & how to solve this error.
 
M

Marc Gravell

try looking at the inner-exceptions; you should usually find a nice
detailed message telling you what the problem is. Either in the IDE,
or add something like below.

Marc

try {
// your code
} catch (Exception ex) {
while(ex != null) { // unroll inner exceptions
Debug.WriteLine(ex.Message);
ex = ex.InnerException;
}
throw;
}
 
U

Udo Nesshoever

=== original message ===
from: Swappy
date: 25.07.2008 09:25
I m trying to serialize the object of the serializable class DsExportDatabase.
But
XmlSerializer serializer = new XmlSerializer(typeof(DsExportDatabase));

This line throws me exception that "There was an error reflecting type "
wherein i have made [Serializable] to the class DsExportDatabase.

Can anybody tell me why this error occure & how to solve this error.

Just adding the attribute serializable to a class doesn't automatically
*make* it serializable. I guess at least one of the properties of your
class is not serializable.

Cheers,
Udo
 
S

Swappy

Marc Gravell said:
try looking at the inner-exceptions; you should usually find a nice
detailed message telling you what the problem is. Either in the IDE,
or add something like below.

Marc

try {
// your code
} catch (Exception ex) {
while(ex != null) { // unroll inner exceptions
Debug.WriteLine(ex.Message);
ex = ex.InnerException;
}
throw;
}



in the inner exception it is giving me erro that "Interface can not serialize"

So to resolve this error without eliminating the interface how can i proceed.
Or other option to proceed.
 
P

Pete Kane

Swappy said:
hi,

I m trying to serialize the object of the serializable class DsExportDatabase.
But
XmlSerializer serializer = new XmlSerializer(typeof(DsExportDatabase));

This line throws me exception that "There was an error reflecting type "
wherein i have made [Serializable] to the class DsExportDatabase.


Can anybody tell me why this error occure & how to solve this error.
I had a similar problem trying to serialize a class based on a DataSet,
the problem was the class didn't have a constructor - just my 2 cents
 
L

Leon Jollans

The only way you can do this is to re-implement the interface as a pure
abstract class (or re-present the interface member in a new serializable
class) and XmlInclude the types in the serializable class. It goes something
like this..

given the following class definitions

public class SerializeMe
{
public SerializeMe(parameters) // ctor
{ ... }

public ISomething InterfaceProperty = new Something();
}

public interface ISomething
{
string Member { get; set; }
}

public class Something : ISomething {
public string Member { get { ... } set{ ... } }
}

you can't serialize this with the XmlSerializer. First, you have to add a
default no-args constructor for SerializeMe. and add an XmlInclude attribute
to the class. Like so

[XmlInclude(typeof(Something))]
public class SerializeMe {
....
}

this tells the serializer to allow for instance of Something to appear in
the XML, despite it not being explicitly specified in the class definition.

This is only part of the trick, since you still can't serialize the
interface. However, if you inject a pure abstract class in between the
ISomething interface and the Something class like this:

public abstract class AbstractSomething : ISomething
{
public abstract string Member { get; set; }
// ... implement all interface properties as abstract properties..
}

public class Something : AbstractSomething
{
public override string Member { get { ... } set{ ... } }
}

[XmlInclude(typeof(Something))]
public class SerializeMe
{
public SerializeMe() {}
public SerializeMe(parameters) // ctor
{ ... }

public AbstractSomething InterfaceProperty = new Something();
}

and now the class will serialize. If you want to have different classes
implementing the interface/abstract base, then you will need to include them
with XmlIncludes at the top of the serializable class.

However, what you *can* do is dynamically generate the XmlIncludes when you
construct the XmlSerializer, but this is a bit trickier, and is left for
another day unless you *really* want to know.

Leon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top