Reputation: 8484
I have two classes
[Serializable]
public class SimpleClass
{
public ComplexClass Parent { get; set; }
}
public class ComplexClass
{
public Guid Id { get; set; }
// Lots of stuff
}
...
// and somewhere
public static List<ComplexClass> ClassesList;
How to serialize SimpleClass
so that only Guid
is saved from complex class? How to deserialize it afterwards? Suppose I already have collection of ComplexClass
es and only need to pick one by id.
I'm using XmlSerializer
for serialization
Upvotes: 0
Views: 173
Reputation: 3008
It would be possible to make a custom serialization of course using ISerializable
, but if you want to keep it simple then something like this might work:
public class SimpleClass
{
[XmlIgnore]
public ComplexClass Parent { get; set; }
public Guid ComplexClassId {
get { return Parent.Id; }
set { Parent = new ComplexClass(value); }
}
}
Or just mark the unwanted fields in ComplexClass using XmlIgnore
.
I have changed my answer to use XmlIgnore
instead of NonSerialized
based on comment from L.B. Left the answer here because I think it still adds some info to Andypopa.
Upvotes: 0
Reputation: 536
Include the Namespace System.Xml.Serialization and adding the attribute [XmlIgnore] over the field or property that you want to be excluded in Serialization.
Upvotes: 1