簡體   English   中英

如何以“一對多”關系序列化兩個對象?

[英]How to serialize two object with `one-to-many` relationship?

我有兩個類: LookupLookupItemLookup具有一個名為Items的成員,該成員是LookupItem的集合。 我無法序列化LookupLookupItem 對於第一個錯誤The type LookupItem was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.我沒有看到The type LookupItem was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 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();
}

更新:類的框架:

[ActiveRecord(Lazy = true)]公共類查找: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)]公共類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; }

}

請注意,我將Catle ActiveRecord用作我的ORM,請注意,此問題與從ActiveRecordBase繼承無關。 因為此域中沒有關系的其他類正常工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM