XElement property is not marked as Serializable

XElement property is not marked as Serializable

Old forum URL: forums.lhotka.net/forums/t/8082.aspx


russelle posted on Wednesday, December 02, 2009

I added a business object property that is of type XElement:

//HouseholdDemographicsXML Property
private static PropertyInfo<XElement> HouseholdDemographicsXMLProperty =

RegisterProperty(typeof(Household), new PropertyInfo<XElement>("HouseholdDemographicsXML",
"Household Demographics XML"));
public XElement HouseholdDemographicsXML
{
get { return GetProperty(HouseholdDemographicsXMLProperty); }
set { SetProperty(HouseholdDemographicsXMLProperty, value); }
}

 

The following loads the property:

LoadProperty(HouseholdDemographicsXMLProperty, XElement.Parse(data.Demographics));

where data.Demographics is a string representing an XML fragment.

The problem is that when I try to instantiate the object I get the message:

Type System.Xml.Linq.XElement in Assembly System.Xml.Linq is not marked as serializable

Does this mean I can't use XElement as a business object property type? Is there a workaround? Could I create a new class inheriting from XElement and somehow mark it as Serializable?

RockfordLhotka replied on Wednesday, December 02, 2009

If a type is not Serializable there's nothing you can do - you can't make someone else's type Serializable.

What you can do is maintain the value itself as a string. Remember that CSLA doesn't serialize properties - it serializes fields. You can have an XElement property, as long as the backing value is of a serializable type (like string).

ctiu replied on Friday, October 15, 2010

Thanks for posting your issue Russelle!

And thanks for the tip Rocky!  This solved my issue.

Here's an example of how I applied your suggestion in my business object...

Public Shared ReadOnly MyXElementDataProperty As PropertyInfo(Of String) = RegisterProperty(New PropertyInfo(Of String)("MyXElementData", "My XElement Data", New XElement("Data")))
Protected Property MyXElementData() As XElement
 Get
  Return XElement.Parse(GetProperty(MyXElementDataProperty))
 End Get
 Set(ByVal value As XElement)
  SetProperty(MyXElementDataProperty, value)
 End Set
End Property 

Regards,

Carlo

 

Copyright (c) Marimer LLC