How to ignore a specific List item when serializing a container class

前端 未结 2 1097
太阳男子
太阳男子 2021-01-24 13:19

I would like to know how to ignore a specific item/index of a List from being serialized using XmlSerializer.

For example, co

2条回答
  •  执笔经年
    2021-01-24 14:00

    Better solution is to create a new collection with LINQ query and pass to serializer, like

    List collection = new List(){ 0, 1, 2, 3 };
    using (var fs = new StreamWriter("serialized.txt"))
    {
        XmlSerializer serializer = new XmlSerializer(collection.GetType());
        serializer.Serialize(fs, collection.Where(x => x != 0).ToList());
    }
    

提交回复
热议问题