Serialization of type that is not known staticaly  
Author Message
Dejan Kocijasevic





PostPosted: ASMX Web Services and XML Serialization, Serialization of type that is not known staticaly Top

Hi,

I have a problem when trying to serialize class with field whose type is not known staticaly.

When I try to do something like:

StatementResponseBody srb = new StatementResponseBody();

srb.Statement = new AccountBalanceType();

srb.Statement.ExtendedAccountInformation = new PaymentAccountExtendedInformation();

srb.Serialize();

exception is thrown with inner exception message:

The type Service3.PaymentAccountExtendedInformation was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

but srb.Statement.Serialize();

serializes srb.Statement as excpected. How can I serialize srb

Class definitions are below.

[Serializable]

public class StatementResponseBody

{

[XmlElement(Namespace = " http://www.hide-link.com/ ;)]

public AccountBalanceType Statement;

internal string Serialize()

{

XmlSerializer ser = new XmlSerializer(this.GetType());

TextWriter tw = new StringWriter();

ser.Serialize(tw, this);

return tw.ToString();

}

}

[Serializable]

public class AccountBalanceType

{

[XmlElement(Namespace = " http://www.hide-link.com/ ;)]

private ExtendedAccountInformationType _extendedAccountInformation;

public ExtendedAccountInformationType ExtendedAccountInformation

{

get { return _extendedAccountInformation; }

set { _extendedAccountInformation = value; }

}

internal string Serialize()

{

XmlSerializer ser = new XmlSerializer(typeof(AccountBalanceType));

TextWriter tw = new StringWriter();

ser.Serialize(tw, this);

return tw.ToString();

}

}

[XmlInclude(typeof(PaymentAccountExtendedInformation))]

[Serializable]

public class ExtendedAccountInformationType

{...}

[Serializable]

public class PaymentAccountExtendedInformation : ExtendedAccountInformationType

{...}



.NET Development15  
 
 
rfreire





PostPosted: ASMX Web Services and XML Serialization, Serialization of type that is not known staticaly Top

You have to add the

[XmlInclude(typeof(PaymentAccountExtendedInformation))]

to the StatementResponseBody class.

Rgds,

Rodrigo


 
 
Dejan Kocijasevic





PostPosted: ASMX Web Services and XML Serialization, Serialization of type that is not known staticaly Top

Rodrigo, thank you for your reply. After adding [XmlInclude(typeof(PaymentAccountExtendedInformation))] attribute to the StatementResponseBody class I still have the same problem and exception description is the same.

However, if [XmlElement(Namespace = http://webservices.pexim.net/CoreBankingAdapter/2006-09-21)] attribute is removed from

public AccountBalanceType Statement; field in StatementResponseBody class, serialization performs without exception and Serialize() method return value is xml that represent StatementResponseBody object. Since I need do define namespace for every field and weather field should serialize as element or as attribute I can not remove XmlElementAttribute from my classes.

Regards,

Dejan


 
 
Steve Strong





PostPosted: ASMX Web Services and XML Serialization, Serialization of type that is not known staticaly Top

Try putting an XmlType attribute on each of your types, i.e., something like:

[XmlType(Namespace=http://webservices.pexim.net/CoreBankingAdapter/2006-09-21)]

public class ....

Cheers,

Steve


 
 
Dejan Kocijasevic





PostPosted: ASMX Web Services and XML Serialization, Serialization of type that is not known staticaly Top

Thank you for your reply Steve. Replacing

[XmlElement(Namespace = "http://webservices.pexim.net/CoreBankingAdapter/2006-09-21")]

with

[XmlElement(Namespace = "http://webservices.pexim.net/CoreBankingAdapter/2006-09-21", Type = typeof(PaymentAccountExtendedInformation))]

in AccountBalanceType class solved the problem

Regards,

Dejan