我有一个简单的2D字符串数组,我想把它填入MOSS中的SPFieldMultiLineText.这映射到ntext数据库字段.
我知道我可以序列化为XML并存储到文件系统,但我想在不触及文件系统的情况下进行序列化.
public override void ItemAdding(SPItemEventProperties properties) { // build the array List> matrix = new List
>(); /* * 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 }
看起来我应该可以做这样的事情:
XmlSerializer s = new XmlSerializer(typeof(List>)); properties.AfterProperties["myNoteField"] = s.Serialize.ToString();
但这不起作用.我发现的所有示例都演示了写入文本文件.
StringWriter outStream = new StringWriter(); XmlSerializer s = new XmlSerializer(typeof(List>)); s.Serialize(outStream, myObj); properties.AfterProperties["myNoteField"] = outStream.ToString();
这是一个通用序列化器(C#):
public string SerializeObject(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(); } }
在你的情况下你可以打电话:
SerializeObject>(matrix);
将TextWriter和TextReader类与StringWriter一起使用.
以机智:
XmlSerializer s = new XmlSerializer(typeof(whatever)); TextWriter w = new StringWriter(); s.Serialize(w, whatever); yourstring = w.ToString();