2010-08-12 69 views
1

我有兩個類:LookupLookupItemLookup有一個名爲Items的成員,它是LookupItem的集合。我無法序列化LookupLookupItem。第一次,我得到錯誤The type LookupItem was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.和第二我得到錯誤A circular reference was detected while serializing an object of type Lookup.如何用「一對多」關係序列化兩個對象?

我該如何解決這個問題?序列化

我使用以下代碼:

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: 骨架的類:

[ActiveRecord的(懶惰=真)] 公共類查找:ActiveRecordExtender,IComparable的 { 公開查詢() { }

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

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

}

[ActiveRecord的(懶惰=真)] 公共類LookupItem:ActiveRecordExtender { 公共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; } 

}

請注意我m使用Catle ActiveRecord作爲我的ORM,請注意這個問題與ActiveRecordBase的繼承無關。因爲這個域中沒有關係的其他類正常工作。

+0

你可以在你的文章中包含Lookup和LookupItem類骨架嗎? – CoderHawk 2010-08-12 06:31:12

+0

@Sandy,請參閱我的更新 – 2010-08-12 06:50:13

回答

0

根據博客文章,其意見here,如果假定沒有必要有關數據顯示,添加[XmlIgnore]解決問題。

相關問題