2008-11-19 8 views
26

J'ai un simple tableau de chaînes 2D et je voudrais l'insérer dans un fichier SPFieldMultiLineText dans MOSS. Cela correspond à un champ de base de données ntext.Sérialisation en C# sans utiliser le système de fichiers

Je sais que je peux sérialiser en XML et stocker dans le système de fichiers, mais je voudrais sérialiser sans toucher au système de fichiers.

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 
} 

On dirait que je devrais être en mesure de faire quelque chose comme ceci:

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

mais cela ne fonctionne pas. Tous les exemples que j'ai trouvés démontrent l'écriture dans un fichier texte.

Répondre

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

DANS 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

passer par un tableau d'octets? Aie. – bzlm

+0

Une des possibilités cependant. Il y a plusieurs façons de le faire! Si vous en avez un meilleur, publiez-le! Et je vais upvote si ça vaut le coup. – JSC

5

Utilisez le TextWriter et les classes de TextReader avec StringWriter.

À savoir:

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

Voici un sérialiseur générique (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(); 
     } 
    } 

Dans votre cas, vous pouvez appeler avec:

SerializeObject<List<string>>(matrix); 
Questions connexes