Serialización en C# sin usar el sistema de archivos


Tengo un simple array 2D de cadenas y me gustaría rellenarlo en un SPFieldMultiLineText en MOSS. Esto se asigna a un campo de base de datos ntext.

Sé que puedo serializar a XML y almacenar en el sistema de archivos, pero me gustaría serializar sin tocar el sistema de archivos.

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
}

Parece que debería ser capaz de hacer algo como esto:

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

Pero eso no funciona. Todos los ejemplos que he encontrado demuestran escribir en un archivo de texto.

Author: Alex Angas, 2008-11-19

4 answers

StringWriter outStream = new StringWriter();
XmlSerializer s = new XmlSerializer(typeof(List<List<string>>));
s.Serialize(outStream, myObj);
properties.AfterProperties["myNoteField"] = outStream.ToString();
 40
Author: Sunny Milenov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-19 18:32:48

Aquí hay un serializador genérico (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();
        }
    }

En tu caso podrías llamar con:

    SerializeObject<List<string>>(matrix);
 12
Author: Harrison,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-19 20:46:24

Utilice las clases TextWriter y TextReader con StringWriter.

A saber:

XmlSerializer s = new XmlSerializer(typeof(whatever));
TextWriter w = new StringWriter();
s.Serialize(w, whatever);
yourstring = w.ToString();
 5
Author: Paul Sonier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-19 18:37:51

EN 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

EN 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);
}
 2
Author: JSC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-19 18:41:31