Serialization in C# without using file system
Asked Answered
K

4

26

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.

Kedron answered 19/11, 2008 at 18:10 Comment(0)
G
40
StringWriter outStream = new StringWriter();
XmlSerializer s = new XmlSerializer(typeof(List<List<string>>));
s.Serialize(outStream, myObj);
properties.AfterProperties["myNoteField"] = outStream.ToString();
Girth answered 19/11, 2008 at 18:32 Comment(0)
H
12

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);
Hardship answered 19/11, 2008 at 20:46 Comment(0)
T
5

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();
Trident answered 19/11, 2008 at 18:37 Comment(0)
B
2

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);
}
Bascinet answered 19/11, 2008 at 18:33 Comment(2)
Going via a byte array? Ouch.Madder
One of the possibilities though. There are several ways to do it! If you have an better one publish it! And I will upvote if it's worth it.Bascinet

© 2022 - 2024 — McMap. All rights reserved.