How to XmlSerialize an Object that has an interface in it

  • Thread starter Thread starter JPK
  • Start date Start date
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
 
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.
 
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.
 
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.
 
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;
}
 
Back
Top