Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization in C# without using file system

I have a simple 2D array of strings and I would like to stuff it into an SPFieldMultiLineText in MOSS. This maps to an ntext database field.

I know I can serialize to XML and store to the file system, but I would like to serialize without touching the filesystem.

public override void ItemAdding(SPItemEventProperties properties)
{
    // build the array
    List<List<string>> matrix = new List<List<string>>();
    /*
    * populating the array is snipped, works fine
    */
    // now stick this matrix into the field in my list item
    properties.AfterProperties["myNoteField"] = matrix; // throws an error
}

Looks like I should be able to do something like this:

XmlSerializer s = new XmlSerializer(typeof(List<List<string>>));
properties.AfterProperties["myNoteField"] = s.Serialize.ToString();

but that doesn't work. All the examples I've found demonstrate writing to a text file.

like image 777
Nathan DeWitt Avatar asked Nov 19 '08 18:11

Nathan DeWitt


People also ask

What is the use of serialize ()?

The serialize() function converts a storable representation of a value. To serialize data means to convert a value to a sequence of bits, so that it can be stored in a file, a memory buffer, or transmitted across a network.

What do we mean with serialization?

Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.

What is serialize in C++?

Serialization is the process of writing or reading an object to or from a persistent storage medium such as a disk file. Serialization is ideal for situations where it is desired to maintain the state of structured data (such as C++ classes or structures) during or after execution of a program.

What are the types of serialization?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization.


4 Answers

StringWriter outStream = new StringWriter(); XmlSerializer s = new XmlSerializer(typeof(List<List<string>>)); s.Serialize(outStream, myObj); properties.AfterProperties["myNoteField"] = outStream.ToString(); 
like image 80
Sunny Milenov Avatar answered Sep 24 '22 15:09

Sunny Milenov


Here's a Generic serializer (C#):

    public string SerializeObject<T>(T objectToSerialize)     {         BinaryFormatter bf = new BinaryFormatter();         MemoryStream memStr = new MemoryStream();          try         {             bf.Serialize(memStr, objectToSerialize);             memStr.Position = 0;              return Convert.ToBase64String(memStr.ToArray());         }         finally         {             memStr.Close();         }     } 

In your case you could call with:

    SerializeObject<List<string>>(matrix); 
like image 25
Harrison Avatar answered Sep 21 '22 15:09

Harrison


Use the TextWriter and TextReader classes with the StringWriter.

To Wit:

XmlSerializer s = new XmlSerializer(typeof(whatever));
TextWriter w = new StringWriter();
s.Serialize(w, whatever);
yourstring = w.ToString();
like image 27
Paul Sonier Avatar answered Sep 20 '22 15:09

Paul Sonier


IN VB.NET

Public Shared Function SerializeToByteArray(ByVal object2Serialize As Object) As Byte()
    Using stream As New MemoryStream
        Dim xmlSerializer As New XmlSerializer(object2Serialize.GetType())
        xmlSerializer.Serialize(stream, object2Serialize)
        Return stream.ToArray()
    End Using
End Function

Public Shared Function SerializeToString(ByVal object2Serialize As Object) As String
    Dim bytes As Bytes() = SerializeToByteArray(object2Serialize)
    Return Text.UTF8Encoding.GetString(bytes)
End Function

IN C#

public byte[] SerializeToByteArray(object object2Serialize) {
       using(MemoryStream stream = new MemoryStream()) {
          XmlSerializer xmlSerializer = new XmlSerializer(object2Serialize.GetType());
          xmlSerializer.Serialize(stream, object2Serialize);
          return stream.ToArray();
       }
}

public string SerializeToString(object object2Serialize) {
   byte[] bytes = SerializeToByteArray(object2Serialize);
   return Text.UTF8Encoding.GetString(bytes);
}
like image 30
JSC Avatar answered Sep 23 '22 15:09

JSC