c#序列化忽略属性的ObservableCollection

时间:2017-12-04 04:00:48

标签: c# xml serialization observablecollection

我正在使用Creators Update SDK开发UWP应用。 我正在尝试序列化ObservableCollection忽略他们班级的属性。

这是我的代码,它有我的类和序列化的方法,你可以看到我正在使用[DataContract][IgnoreDataMember],但它不起作用。

public class Classes
    {
        [DataContract]
        public class Car : BindableBase
        {
            [DataMember]
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set { Set(ref _Name, value); }
            }

            [DataMember]
            private string _Brand;
            public string Brand
            {
                get { return _Brand; }
                set { Set(ref _Brand, value); }
            }

            [IgnoreDataMember]
            private bool _Electric;
            public bool Electric
            {
                get { return _Electric; }
                set { Set(ref _Electric, value); }
            }

            [DataMember]
            private double _Price;
            public double Price
            {
                get { return _Price; }
                set { Set(ref _Price, value); }
            }
        }

        public class Values_Car : ObservableCollection<Car> { }

        public static class Garage
        {
            public static Values_Car Cars = new Values_Car();

            static Garage()
            {
            }
        }

        [XmlRoot("Root")]
        public class GarageDTO
        {
            [XmlElement]
            public Values_Car Cars { get { return Garage.Cars; } }
        }
    }

   public class NewSerialization
    {
        private static void FillList()
        {
            Car e_1 = new Car()
            {
                Name = "element_Name",
                Brand = "element_Brand",
                Electric = true,
                Price = 1,
            };
            Car e_2 = new Car()
            {
                Name = "element_Name",
                Brand = "element_Brand",
                Electric = true,
                Price = 2,
            };

            Garage.Cars.Add(e_1);
            Garage.Cars.Add(e_2);
        }

        public static string Serializer()
        {
            FillList();

            var _Instance = new GarageDTO();

            var serializer = new XmlSerializer(typeof(GarageDTO));

            using (var stream_original = new MemoryStream())
            {
                serializer.Serialize(stream_original, _Instance);

                string string_original = string.Empty;

                stream_original.Position = 0;

                using (StreamReader reader = new StreamReader(stream_original, Encoding.Unicode))
                {
                    string_original = reader.ReadToEnd();
                }

                return string_original;
            }
        }
    }

使用NewSerialization.Serializer();我得到了: 但是在xml中我得到了Electric属性,这个属性被忽略了。

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Cars>
    <Name>element_Name</Name>
    <Brand>element_Brand</Brand>
    <Electric>true</Electric>
    <Price>1</Price>
  </Cars>
  <Cars>
    <Name>element_Name</Name>
    <Brand>element_Brand</Brand>
    <Electric>true</Electric>
    <Price>2</Price>
  </Cars>
</Root>

如何在序列化中忽略ObservableCollection的属性?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

感谢@dbc和@Aluan Haddad的评论,我发现了我找到的确切内容,这应该是我的课程:

public class Car : BindableBase
        {
            private string _Name;
            public string Name
            {
                get { return _Name; }
                set { Set(ref _Name, value); }
            }

            private string _Brand;
            public string Brand
            {
                get { return _Brand; }
                set { Set(ref _Brand, value); }
            }

            private bool _Electric;
            [XmlIgnore] //<----This!           
            public bool Electric
            {
                get { return _Electric; }
                set { Set(ref _Electric, value); }
            }

            private double _Price;
            public double Price
            {
                get { return _Price; }
                set { Set(ref _Price, value); }
            }            
        }