[英]How to serialize two object with `one-to-many` relationship?
I have two classes: Lookup
and LookupItem
that Lookup
has a member named Items
that is a collection of LookupItem
s. 我有两个类:
Lookup
和LookupItem
, Lookup
具有一个名为Items
的成员,该成员是LookupItem
的集合。 I'm not able to serialize Lookup
or LookupItem
. 我无法序列化
Lookup
或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.
对于第一个错误
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.
and with the second I got error A circular reference was detected while serializing an object of type Lookup.
第二个错误
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() { } [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)] public class LookupItem : ActiveRecordExtender { public LookupItem() { } [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; }
} }
Please note I'm using Catle ActiveRecord as my ORM and please notice this problem is not related to inheritance from ActiveRecordBase
. 请注意,我将Catle ActiveRecord用作我的ORM,请注意,此问题与从
ActiveRecordBase
继承无关。 Because other classes in this domain that has no relations work properly. 因为此域中没有关系的其他类正常工作。
I assume you want to serialize nested classes. 我假设您要序列化嵌套类。
Have a look at similar post C# System.Xml.Serialization Self-nested elements 看看类似的帖子C#System.Xml.Serialization自嵌套元素
http://www.codeproject.com/KB/XML/Serializing_Nested_XML.aspx http://www.codeproject.com/KB/XML/Serializing_Nested_XML.aspx
根据博客文章,其意见在这里 ,如果假定没有必要有关数据显示,添加[XmlIgnore]
解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.