2

I have an C# object that hold a big list (10-100MB) and some other properties. I want to serialize the object, but I don't want to serialize the list. is there any easy way to do that?

Thanks!

1
  • 1
    What type of serialization do you want? XML, Binary,... Commented Nov 17, 2013 at 8:17

2 Answers 2

5

Since it is tagged , this could be as simple as adding [XmlIgnore] to the appropriate property:

[XmlIgnore]
public List<Foo> Items {get;set;}

Then just use XmlSerializer as normal.

If it needs to be controllable (sometimes yes, sometimes no) then an alternative is to add:

public bool ShouldSerializeItems() {
    // your logic here
}

This pattern is recognised by many serializers, XmlSerializer included.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the first option seems to work :) can you explain again the second one?
@Amir the serializer basically checks for a few name-based patterns it cab use. If there is a public bool ShouldSerialize*() method (for the appropriate named member), it calls that method during serialization to decide whether to serialize that member or not
Simiarly, the serializer checks for a property named *Specified (for the appropriate named member).
@Brian that is double edged: the plus is that the setter can be hijacked to acknowledge data; however, properties often interfere with other binding frameworks - only some of which check for [Browsable(false)] etc
0

If you mean by serialization loading the object from database, then I can recommend NHibernate, you simply mark, you want to enable lazy-loading on the List and your object will return, with Items set to null (and if you are still in transaction, Items will load, after and only after attempting to access it; if you are out of transaction already, you will get a nullPointer exception I think)... but basically, Lazy-loading in any its kind is certainly a principle, you should keep an eye on.

EDIT: missed the tags, shame on me

1 Comment

Serialization usually means "to a blob/clob". Database work is usually "persistence/materialisation". But yes, the terms are sometimes used rather interchangeably.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.