C#中的序列化而不使用文件系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/302821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:22:32  来源:igfitidea点击:

Serialization in C# without using file system

c#sharepointserializationmosswss

提问by Nathan DeWitt

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.

我有一个简单的 2D 字符串数组,我想将它填充到 MOSS 中的 SPFieldMultiLineText 中。这映射到 ntext 数据库字段。

I know I can serialize to XML and store to the file system, but I would like to serialize without touching the filesystem.

我知道我可以序列化为 XML 并存储到文件系统,但我想在不接触文件系统的情况下进行序列化。

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.

但这不起作用。我发现的所有示例都演示了写入文本文件。

采纳答案by Sunny Milenov

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

回答by JSC

IN VB.NET

在 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#

在 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);
}

回答by Paul Sonier

Use the TextWriter and TextReader classes with the StringWriter.

将 TextWriter 和 TextReader 类与 StringWriter 一起使用。

To Wit:

以机智:

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

回答by Harrison

Here's a Generic serializer (C#):

这是一个通用序列化程序(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);