具有多层数据结构的DataContractSerializer [英] DataContractSerializer with Multi-tiered data structures

查看:292
本文介绍了具有多层数据结构的DataContractSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个 DataContractSerializer 来生成一个表示我们稍微复杂的对象模型的XML文件。根对象具有多个 ICollection 属性,其中一个具有多个 ICollection 属性,其中的多个具有多个 ICollection 属性...你得到这个想法。



我用 [DataContract (Name =foo)] 标签,请阅读这个关于使用 Include()的问题,并开始制定它。当我把顶层放在一起时,我想知道第二层是否也需要明确的声明。这是我迄今为止所做的:

  public void Serialize(string DataCode)
{
Product prod = context.Products
.Include(p => p.Products)
.Include(p => p.References)
.Include(p => p.Dates)
.Include(p => p.Weights)
.Include(p => p.Notes)
.Include(p => p.Rules)//规则有PriceConditions,有价格...
.Include(p => p.DataBooks)
.First(m => m.ProductCode == DataCode);
FileStream fs = new FileStream(path,FileMode.Create);
XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
DataContractSerializer serializer = new DataContractSerializer(typeof(Product));
serializer.WriteObject(writer,prod);
}

所以,我需要做一些规则,以确保整个结构被写入,或者 Include()知道深入结构并加载每个元素?

 产品prod =上下文。产品
.Include(p => p.Rules.Select(r => r.PriceConditions.Select(p => p.Prices)));




I'm attempting to implement a DataContractSerializer to generate an XML file that represents our somewhat complicated object model. The root object has multiple ICollection properties, one of which has multiple ICollection properties, multiples of which have multiple ICollection properties... you get the idea.

I decorated all my relevant classes with [DataContract(Name = "foo")] tags, read this question about using Include(), and started framing it out. As I put together the top layer, I wondered if the second layer would need explicit declarations as well. Here's what I have so far:

    public void Serialize(string DataCode)
    {
        Product prod = context.Products
            .Include(p => p.Products)
            .Include(p => p.References)
            .Include(p => p.Dates)
            .Include(p => p.Weights)
            .Include(p => p.Notes)
            .Include(p => p.Rules)  // Rules have PriceConditions, which have Prices...
            .Include(p => p.DataBooks)
            .First(m => m.ProductCode == DataCode);
        FileStream fs = new FileStream(path,FileMode.Create);
        XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs);
        DataContractSerializer serializer =  new DataContractSerializer(typeof(Product));
        serializer.WriteObject(writer, prod);
    }

So, do I need to do something with Rules in order to ensure that the entire structure gets written, or does Include() know to tunnel deep into the structure and load each element?

解决方案

You would need to explicitly include the lower levels as well.

Product prod = context.Products
    .Include(p => p.Rules.Select(r => r.PriceConditions.Select(p => p.Prices)));

这篇关于具有多层数据结构的DataContractSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆