Serialization in C# without using file system

Nathan DeWitt picture Nathan DeWitt · Nov 19, 2008 · Viewed 23k times · Source

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.

Answer

Sunny Milenov picture Sunny Milenov · Nov 19, 2008
StringWriter outStream = new StringWriter();
XmlSerializer s = new XmlSerializer(typeof(List<List<string>>));
s.Serialize(outStream, myObj);
properties.AfterProperties["myNoteField"] = outStream.ToString();