Ich entwickle eine UWP-App mit dem Creators Update SDK. Ich versuche, eine ObservableCollection
Ignorieren einer Eigenschaft ihrer Klasse zu serialisieren.C# Serialisieren einer ObservableCollection Ignorieren einer Eigenschaft
Hier ist mein Code, es habe meine Klasse und die Methoden zu serialisieren, können Sie sehen, ich verwende [DataContract]
und [IgnoreDataMember]
, aber es funktioniert nicht.
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();
mit Ich habe: Aber im xml Ich habe die Electric
Eigenschaft, die ignoriert wird.
<?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>
Wie kann ich eine Eigenschaft meiner ObservableCollection
auf die Serialisierung zu ignorieren?
Schätzen Sie Ihre Hilfe.
Read [Attribute-that-control-xml-Serialisierung] (https://docs.microsoft.com/en-us/dotnet/standard/serialization/attributes-that-control-xml-serialization) –
Sie verwenden 'XmlSerializer' aber' [DataContract] ',' [DataMember] ' und '[IgnoreDataMember]' sind für ['DataContractSerializer'] (https://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx). Siehe [Attribute, die die XML-Serialisierung steuern] (https://docs.microsoft.com/en-us/dotnet/standard/serialization/attributes-thatcontrol-xml-serialization) für die relevanten Attribute. Die gewünschte ist ['[XmlIgnore]'] (https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlignoreattribute (v = vs.110) .aspx). – dbc