Integrate from DataContract to IXmlSerializable using NetDataContractSerializer - c#

I have some data that is persisted in a database. The serialized content was originally written using the default NetDataContractSerializer with a DataContract attribute on the classes.
Now I want to move to using classes that implement IXmlSerializable to have more control over the serialized content and make it leaner and faster.
How will I be able to read the current content as well as store future content in the new way.
I have looked into ISurrogateSelector, but it doesn't seem to do the trick as the ISerializationSurrogate interface only supports getting and setting object data as the ISerializable interface, but no way to specify ReadXml and WriteXml as in IXmlSerializable interface. As I am reading in the stream, I don't have any information about the format other than the stream itself.
The deserialization must produce the same class instance regardless of the original serialization method.
I need to make a solution that uses .NET Framework 4.6

Related

How are methods returning a Stream implemented?

The .NET framework is full of examples where a method call will return a Stream that you can then read and implement as you need to. But how does this work under the covers? What backs the stream?
Say I am writing a parser that takes some inputs and parses some data into a pre-defined format. If for example I created a MemoryStream and then write my content to it using a StreamWriter and then have the method return the stream, I will run into issues because the writer will close the underlying stream when it completes and the caller won't be able to read it as expected.
How is this typically managed? Is the content for the stream stored in the object until needed (like a byte[]) and then when the method requesting the Stream is invoked it creates it at that time or what?
A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or a TCP/IP socket. The Stream class and its derived classes provide a generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices.
[MSDN said]
so i suppose you using serialize , for Serialization using different formats in .NET by using stream need to define your demands
Serialization is the process of converting an object in to bytes for persistence storage. The deserialization process converts the bytes to object without any loss of data. Serialization is used for storing values in files or database, send an object through the network and to convert back to the original object format. The .NET Framework provide a set of Framework Class Libraries (FCL) for making the serialization process easy. It is very useful for sending data between two different applications.
The .NET Framework supports binary serialization and XML serialization formats. XML serialization serializes only public fields. But, binary serialization will serialize all private and public fields. Serialization can be performed either as basic or custom. Basic serialization happens when a class has the SerializableAttribute attribute applied. Basic serialization doesn't support for versioning. A custom serialization class must be marked SerializableAttribute and implement the ISerializable interface. The user can implement custom serialization for both Binary and XML serialization formats. GetObjectData needs to be overridden for a custom application. The sample application uses custom serialization for both binary and XML serialization. The .NET Framework supports designer serialization which is associated with development tools.
Custom serialization
Custom serialization is the process of controlling the serialization and deserialization process. Custom serialization can be implemented by running custom methods during and after serialization or by implementing the ISerializable interface. Custom serialization is used for versioning the serialization object. If the serialized object has changed the object state (added a new file in later version), custom serialization is used for getting the values without loss of data. The versioning of the serialized object may fail due to missing attributes.
If the user wants to use custom methods during and after serialization, the user should apply the custom serialization support using OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute and OnSerializingAttribute attributes for customizing the data during serialization and deserialization. The OptionalFieldAttribute attribute is used for ignoring the old version data for deserialization. The formatter doesn't give any error during deserialization. It allows for updating the object before and after serialization/deserialization.
i think below link help you
http://www.codeproject.com/Articles/422474/Serialization-using-different-formats-in-NET
The stream can be backed by many different things. That's the whole idea of streams deriving from the Stream abstract base class.
The stream can be backed by an OS level file stream, by memory, by an HTTP connection, or anything else that can fulfill the Stream contract.
In the case of a MemoryStream the backing storage is just a block of memory.
In the case of StreamWriter, calling Dispose() on it will close the underlying stream. Make sure you don't dispose the writer as long as you still want to use the stream. Also, if you want to re-access a MemoryStream after writing to it, be sure and set the position to the beginning, e.g.:
memStream.Seek(0, SeekOrigin.Begin);
StreamWriter has an overloaded constructor that you can use to instruct the write to not close the stream.
Also, the Stream should have a .WriteBytes method that allows you to avoid a StreamWriter altogether.

save c# object to disk

I want to save/read an object to disk in c#. some of it's properties are not marked with [Serializable].
Is there any other way to save it?
I don't care about the format of the object on the disk, I only want to dump it to the disk and read it.
Any suggestions?
An alternative to the suggested XmlSerializer would be protobuf-net. It's a .net implementation of Google's protobuf written by a SO/StackExchange own Marc Gravell.
Note, you will have to have access to the objects since protobuf-net supports only custom classes that:
are marked as data-contract
have a parameterless constructor
for Silverlight: are public
If that doesn't work for you, you could either write your own solution or, depending on your scope, use an object db like eloquera.
The XmlSerializer class does not require an object to be marked as serializable, but it only saves the public fields and properties. If you need to serialize the private members, you are out of luck unless you write your own serializer.

c#: make a 3rd party object serializable

On my own object I can add the metatag [Serializable] to make it serializable. Now I use a 3rd party library that I need to be serializable. I inspected the code and it should not be a problem. Is there a way to fix this without altering the 3rd party code?
My advice would be: serialize data, not implementation. The fact of the existence of a 3rd-party object is nothing to do with the data; that is an implementation detail. As such, I always offer the same advice: if serialization ever gets complex, the first thing to do is to introduce a separate DTO model that represents the data in isolation of the implementation, and just map the current state to that DTO. This allows you to handle implementation changes without impact on the storage, and allows otherwise non-serializable objects to be serialized.
Some serializers offer workarounds - for example with protobuf-net you can a: supply the serialization information for any type at runtime, and b: supply a "surrogate" to use automatically when it gets tricky, but - using a DTO model is simpler and easier to maintain.
Your use of [Serializable] suggests BinaryFormatter; in my opinion, this is almost never a good choice for any kind of storage, since BinaryFormatter relies on implementation details. It works nicely for passing data between two in-sync app-domains, though
If the types are public you should be able to use the XmlSerializer to do what you want.
There's more information on this here
Serializes and deserializes objects into and from XML documents. The
XmlSerializer enables you to control how objects are encoded into XML.
Exactly take your subclass and make it serializable.
[Serializable] public class Foo: Bar {}
Write an adapter or be prepared to do something more extreme like disassembling the assembly, injecting the serializable attribute and reassembling.

What XML serialization method should I use for a public API?

I'm writing a program that builds up a tree structure made up of classes that inherit from an abstract Node class. There are a number of different type of nodes built into my program. However, I also want to allow more advanced users to be able to reference my library and write their own derivations of Node. These plug-in libraries are then loaded when my app starts up through Assembly.Load(). Thus all the potential Node types used by my application will not be known until run time.
In addition, I want to be able to serialize and deserialize these trees to and from XML files. I have some experience with XMLSerializer, DataContractSerializer, and implementing IXmlSerializable. Typically, I go with DataContractSerializer as it usually requires less code then implementing IXmlSerializable, and can serialize private fields where XmlSerializer can not.
Yet with this project I also have to consider that other users will be creating classes that derive from my class, and will also have to add whatever code or attributes are required to serialize them as well.
Considering this are there reasons I should go with one serialization mechanism over another?
If the serialization and deserialization will only occur within your application, and if there is no requirement that anyone else be able to read the serialized data, then the serialization format doesn't impact the API: as far as a user of the API is concerned, you will serialize into an opaque file and deserialize from the same.
In this case, use DataContractSerializer, as it can serialize into binary if necessary.

C# serialization question

Is there any way to serialize/deserialize an object DataContractJsonSerializer as well as ISerializeable interface. as my requirement is I get JSon from a web service and deserialize it into a collections of objects say message. now I want to write all objects in the list to file.. is there any way please explain.
The DataContractJsonSerialiazer uses the DataContract / DataMember attributes, and doesn't care about ISerializable. Nothing prevents you from adding both the DataContract attribute and implemented the ISerializable interface, though.
You could also use the System.Web.Script.Serialization.JavaScriptSerializer class instead, I believe that it respects ISerializable. It isn't very fast though, and doesn't support dates, so I would really not recommend it.
Are you sure you cannot just use the DataContract serializer? You can use that with a binary writer if you want to: http://msdn.microsoft.com/en-us/library/ms752244.aspx

Categories