我可以动态地应用XmlIgnore到ArrayItem的属性? [英] Can I dynamically apply XmlIgnore to properties of an ArrayItem?

查看:163
本文介绍了我可以动态地应用XmlIgnore到ArrayItem的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想序列化列表< T>其中T:EntityObject,很想离开了所有的EntityKey和其他的EntityReference属性从列表中的项目。可以这样做动态可能使用XmlAttributeOverrides?

据我所看到的,XmlAttributeOverrides选项只有真正指向即名单,其中顶层的对象; T>而不是牛逼自己,这是不是对我很有帮助。 任何人都可以点我的方式来动态地忽略ArrayItems的属性?

下面是一个简单的例子,我一直在使用,不使用EntityObjects但应该说明我想要做的:

 公共类车
    {
        公共字符串制作{获得;组; }
        公共字符串型号{获得;组; }
        公共双EngineSize {获得;组; }
    }

    [测试]
    公共无效WouldLoveToDynamicallyLeaveOutMembersOfArrayItems()
    {
        VAR汽车=新的名单,其中,汽车及GT;
                       {
                         新汽车
                             {
                                 请=法拉利,
                                 模型=F1,
                                 EngineSize = 6000
                             },
                         新汽车
                             {
                                 请=威廉姆斯
                                 模型=F1,
                                 EngineSize = 5500
                             }
                     };


        VAR attributeOverrides =新XmlAttributeOverrides();
        attributeOverrides.Add(typeof运算(双人间),EngineSize,新XMLATTRIBUTES {XmlIgnore = TRUE});

        VAR XS =新的XmlSerializer(cars.GetType(),attributeOverrides,新的[] {typeof运算(车)},新XmlRootAttribute(汽车总动员),);
        VAR毫秒=新的MemoryStream();
        xs.Serialize(MS,汽车);
        ms.Position = 0;

        VAR SR =新的StreamReader(MS);
        VAR的结果= sr.ReadToEnd();

        Assert.IsFalse(result.Contains(EngineSize));

    }
 

解决方案

是的,你可以做到这一点 - 主要错误是你需要的,而不是typeof运算(双)typeof运算(车)。与此,请注意,XmlAttributeOverrides的只是应用于顶层onject

不过,我不知道这是最简单的途径。首先,请注意您的必须存储和再利用使用XmlAttributeOverrides时,否则你会泄漏组件的串行器。

我的期望的,你想这样做的主要原因是因为你不想编辑自己生成的文件。然而,还有另一种方式;在一个单独的文件,在正确的命名空间,你可以使用:

 部分类车{
    公共BOOL ShouldSerializeEngineSize(){返回false; }
}
 

在哪里ShouldSerialize *是识别和使用XmlSerializer的控制有条件的序列化模式。在部分类只是将两个独立的code文件合并成一个单一类型的一种方式(并为此设计的生成内容的情况)。

这样,你不需要惹XmlAttributeOverrides,并且可以使用更简单的新XmlSeralizer(类型)(其中有每型自动缓存)。

I am trying to serialize a List<T> where T: EntityObject and would love to leave out all the EntityKey and other EntityReference properties from the items in the list. Can this be done dynamically possibly using XmlAttributeOverrides?

As far as I can see, the XmlAttributeOverrides options only really point to the top level object ie the List<T> and not the T themselves, which is not very helpful to me. Could anyone point me to a way to dynamically ignore properties of ArrayItems?

Here is a simple example I have been using that does not use EntityObjects but it should illustrate what I would like to do:

    public class Car
    {
        public String Make { get; set; }
        public String Model { get; set; }
        public Double EngineSize { get; set; }
    }

    [Test]
    public void WouldLoveToDynamicallyLeaveOutMembersOfArrayItems()
    {
        var cars = new List<Car>
                       {
                         new Car
                             {
                                 Make = "Ferrari",
                                 Model = "F1",
                                 EngineSize = 6000
                             },
                         new Car
                             {
                                 Make = "Williams",
                                 Model = "F1",
                                 EngineSize = 5500
                             }
                     };


        var attributeOverrides = new XmlAttributeOverrides();
        attributeOverrides.Add(typeof(Double), "EngineSize", new XmlAttributes {XmlIgnore = true});

        var xs = new XmlSerializer(cars.GetType(), attributeOverrides, new []{ typeof(Car) }, new XmlRootAttribute("cars"), "");
        var ms = new MemoryStream();
        xs.Serialize(ms, cars);
        ms.Position = 0;

        var sr = new StreamReader(ms);
        var result = sr.ReadToEnd();

        Assert.IsFalse(result.Contains("EngineSize"));

    }

解决方案

Yes you can do that - the main error is you need typeof(Car) instead of typeof(double). With this, note that XmlAttributeOverrides does not just apply to the top-level onject.

However, I'm not sure that is the easiest route. Firstly, note that you must store and re-use the serialiser when using XmlAttributeOverrides otherwise you will leak assemblies.

I expect the main reason you want to do this is because you don't want to edit he generated file. However, there is another way; in a separate file, in the right namespace, you can use:

partial class Car {
    public bool ShouldSerializeEngineSize() { return false; }
}

Where "ShouldSerialize*" is a pattern recognised and used by XmlSerializer to control conditional serialization. The "partial" class is simply a way of combining two separate code files into a single type (and was designed for this generated-content scenario).

This way, you dont need to mess with XmlAttributeOverrides, and you can use the simpler "new XmlSeralizer(Type)" (which has automatic caching per-type).

这篇关于我可以动态地应用XmlIgnore到ArrayItem的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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