2008-11-19 15 views
26

Tengo una matriz 2D simple de cadenas y me gustaría incluirla en un SPFieldMultiLineText en MOSS. Esto se asigna a un campo de base de datos ntext.Serialización en C# sin usar el sistema de archivos

Sé que puedo serializar en 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 yo 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 quiere trabajar. Todos los ejemplos que he encontrado demuestran escribir en un archivo de texto.

Respuesta

40
StringWriter outStream = new StringWriter(); 
XmlSerializer s = new XmlSerializer(typeof(List<List<string>>)); 
s.Serialize(outStream, myObj); 
properties.AfterProperties["myNoteField"] = outStream.ToString(); 
2

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); 
} 
+0

Yendo a través de una matriz de bytes? Ay. – bzlm

+0

Sin embargo, una de las posibilidades. ¡Hay varias formas de hacerlo! ¡Si tienes uno mejor, publícalo! Y renunciaré si vale la pena. – JSC

5

Uso del TextWriter y clases TextReader con el StringWriter.

A saber:

XmlSerializer s = new XmlSerializer(typeof(whatever)); 
TextWriter w = new StringWriter(); 
s.Serialize(w, whatever); 
yourstring = w.ToString(); 
12

He aquí 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 su caso se podría llamar con:

SerializeObject<List<string>>(matrix); 
Cuestiones relacionadas