连接的实体未使用 DataContractSerializer 序列化
我正在使用 LINQ-to-SQL,并将序列化模式设置为单向。
我有两个实体类,Country
和 City
,具有一对多关系。
DataContractSerializer serializer =
new DataContractSerializer(typeof(IList<Country>), new[] {
typeof(EntitySet<City>) // I have also tried with typeof(City)
});
string xml;
using (Db db = new Db()) {
IList<Country> countries = db.Countries.ToList();
// ... some manipulation of the list here, add/remove etc ...
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
serializer.WriteObject(writer, countries);
xml = sb.ToString();
}
生成的 XML 字符串包含 Country
实体,但没有 City
实体。
我该如何解决这个问题?
I am using LINQ-to-SQL and I have the Serialization Mode set to Unidirectional.
I have two entity classes, Country
and City
, with a one-to-many relationship.
DataContractSerializer serializer =
new DataContractSerializer(typeof(IList<Country>), new[] {
typeof(EntitySet<City>) // I have also tried with typeof(City)
});
string xml;
using (Db db = new Db()) {
IList<Country> countries = db.Countries.ToList();
// ... some manipulation of the list here, add/remove etc ...
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
serializer.WriteObject(writer, countries);
xml = sb.ToString();
}
The resulting XML string has Country
entities but no City
entities in it.
How do I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
ToList()
似乎导致了这个问题。Calling
ToList()
seemed to cause the problem.