How to XmlSerialize an Object that has an interface in it

J

JPK

I am having trouble with this. Here is a simle example

class A
{

string a;
ISomeInterface x;
}

....

XmlSerializer xmlSer = new XmlSerializer(typeof(A));

Exception:: + InnerException {"Cannot serialize member x of type
A, because it is an interface."}


I have tried [NonSerialized] but it doesn't work on interfaces

Thanks,

JIM
 
B

Bjørn Brox

JPK skrev:
I am having trouble with this. Here is a simle example

class A
{

string a;
ISomeInterface x;
}

...

XmlSerializer xmlSer = new XmlSerializer(typeof(A));

Exception:: + InnerException {"Cannot serialize member x of
type A, because it is an interface."}


I have tried [NonSerialized] but it doesn't work on interfaces
Use [XmlIgnore] in front of it.
 
J

JPK

I added that both in the class AND in the Interface but I still get the same
error.

XmlSerializer is Ignoring XmlIgnore


JIM

Bjørn Brox said:
JPK skrev:
I am having trouble with this. Here is a simle example

class A
{

string a;
ISomeInterface x;
}

...

XmlSerializer xmlSer = new XmlSerializer(typeof(A));

Exception:: + InnerException {"Cannot serialize member x of type
A, because it is an interface."}


I have tried [NonSerialized] but it doesn't work on interfaces
Use [XmlIgnore] in front of it.
 
J

Jeff Johnson

XmlSerializer is Ignoring XmlIgnore

Funny, I always use [XmlIgnoreAttribute].

Your code should look like this:

class A
{

string a;

[XmlIgnoreAttribute]
ISomeInterface x;
}

When using any class derived from Attribute which ends with "Attribute" (and
I can't remember if they're required to or not) you can always drop that
"Attribute," so [XmlIgnore] and [XmlIgnoreAttribute] are identical.
 
J

JPK

My mistake, I missed one spot in a parent class

thanks,,


JIM

Christian Treffler said:
JPK said:
XmlSerializer is Ignoring XmlIgnore

Funny, I always use [XmlIgnoreAttribute].

Your code should look like this:

class A
{

string a;

[XmlIgnoreAttribute]
ISomeInterface x;
}
 

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