Serializing POCO Without Class Members

I want to make POCO [Serializable], but not any other class members in the class hierarchy tree. I know that there is [NonSerialized] that works only for fields, but is there a way to exclude them or select specific members using [Serializable] in POCO?

+4
source share
1 answer

You should look at DataContractSerializer ; it uses the opt-in approach for serialization.

It would also be nice to read XmlSerializer vs DataContractSerializer: Serialization in Wcf for examples and comparisons between DataContractSerializer and XmlSerializer .

XmlSerializer has been in .Net since version 1.0 and serves us well for everything from Remoting, Web Services, serialization to file, etc. However, in .Net 3.0 came DataContractSerializer. And all of a sudden, a lot of guidance suggests that we should use its old proven XmlSerializer. Wcf even uses this as the default serialization mechanism. Question: "Is it really better?" The verdict is yes and no. Like most, it all depends on your implementation and what you need. For Wcf, you should prefer to use the DataContractSerializer. If you need full control over what xml looks like though, you should go back to XmlSerializer.

+4
source

Source: https://habr.com/ru/post/1286416/


All Articles