繁体   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