views:

36

answers:

2

I have two classes: Lookup and LookupItem that Lookup has a member named Items that is a collection of LookupItems. I'm not able to serialize Lookup or LookupItem. With the first I get error The type LookupItem was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. and with the second I got error A circular reference was detected while serializing an object of type Lookup..

How can I solve this problem?

I use following code for serialization:

public static string Serialize(object obj)
{
    XmlSerializer ser = new XmlSerializer(obj.GetType());
    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb);
    ser.Serialize(writer, obj);
    return sb.ToString();
}

UPDATE: skeleton of classes:

[ActiveRecord(Lazy = true)] public class Lookup : ActiveRecordExtender, IComparable { public Lookup() { }

[Property]
public virtual string Title { set; get; } 

// creating relation
[HasMany(typeof(LookupItem), Cascade = ManyRelationCascadeEnum.All)]
public virtual IList Items { set; get; }

}

[ActiveRecord(Lazy = true)] public class LookupItem : ActiveRecordExtender { public LookupItem() { }

//creating relation
[BelongsTo("Lookup_ID")]
public virtual Lookup ContainerLookup { set; get; }

[Property]
public virtual string Title { set; get; } 

[Property]
public virtual string Value { set; get; } 

[Property]
public virtual int SortOrder { set; get; }

}

Please note I'm using Catle ActiveRecord as my ORM and please notice this problem is not related to inheritance from ActiveRecordBase. Because other classes in this domain that has no relations work properly.

+1  A: 

I assume you want to serialize nested classes.

Have a look at similar post http://stackoverflow.com/questions/2851454/c-system-xml-serialization-self-nested-elements

http://www.codeproject.com/KB/XML/Serializing_Nested_XML.aspx

Sandy
none of two links helped me
afsharm
A: 

According to a blog post and its comments here and if assume there is no need to related data, adding [XmlIgnore] solves the problem.

afsharm