您可以使用BinaryFormatter。对于想要一个小文件来说,这是一个很好的解决方案,但只有您知道它是否是您的域的最佳解决方案。不过,我不认为你可以一次读一张唱片。
我目前唯一的示例代码是DataSet。这些扩展方法将(反)序列化自定义数据集,如果我没记错的话,这是拥有可以使用 BinaryFormatter 的类型的最简单方法。
public static TDataSet LoadBinary<TDataSet>(Stream stream) where TDataSet : DataSet
{
var formatter = new BinaryFormatter();
return (TDataSet)formatter.Deserialize(stream);
}
public static void WriteBinary<TDataSet>(this TDataSet dataSet, Stream stream) where TDataSet : DataSet
{
dataSet.RemotingFormat = SerializationFormat.Binary;
var formatter = new BinaryFormatter();
formatter.Serialize(stream, dataSet);
}
您还可以查看DataContractSerializer,这是 .NET 处理序列化的新“标准”方式(根据 C# 4.0 In A Nutshell, Albahari & Albahari)。在这种情况下,您还需要阅读最佳实践:数据合同版本控制。以下是如何在 XML 和 JSON 中(反)序列化的示例,即使它们不会直接适用于您的情况(因为您需要小文件)。但是你可以压缩文件。
/// <summary>
/// Converts this instance to XML using the <see cref="DataContractSerializer"/>.
/// </summary>
/// <typeparam name="TSerializable">
/// A type that is serializable using the <see cref="DataContractSerializer"/>.
/// </typeparam>
/// <param name="value">
/// The object to be serialized to XML.
/// </param>
/// <returns>
/// Formatted XML representing this instance. Does not include the XML declaration.
/// </returns>
public static string ToXml<TSerializable>(this TSerializable value)
{
var serializer = new DataContractSerializer(typeof(TSerializable));
var output = new StringWriter();
using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })
{
serializer.WriteObject(writer, value);
}
return output.GetStringBuilder().ToString();
}
/// <summary>
/// Converts this instance to XML using the <see cref="DataContractSerializer"/> and writes it to the specified file.
/// </summary>
/// <typeparam name="TSerializable">
/// A type that is serializable using the <see cref="DataContractSerializer"/>.
/// </typeparam>
/// <param name="value">
/// The object to be serialized to XML.
/// </param>
/// <param name="filePath">Path of the file to write to.</param>
public static void WriteXml<TSerializable>(this TSerializable value, string filePath)
{
var serializer = new DataContractSerializer(typeof(TSerializable));
using (var writer = XmlWriter.Create(filePath, new XmlWriterSettings { Indent = true }))
{
serializer.WriteObject(writer, value);
}
}
/// <summary>
/// Creates from an instance of the specified class from XML.
/// </summary>
/// <typeparam name="TSerializable">The type of the serializable object.</typeparam>
/// <param name="xml">The XML representation of the instance.</param>
/// <returns>An instance created from the XML input.</returns>
public static TSerializable CreateFromXml<TSerializable>(string xml)
{
var serializer = new DataContractSerializer(typeof(TSerializable));
using (var stringReader = new StringReader(xml))
using (var reader = XmlReader.Create(stringReader))
{
return (TSerializable)serializer.ReadObject(reader);
}
}
/// <summary>
/// Creates from an instance of the specified class from the specified XML file.
/// </summary>
/// <param name="filePath">
/// Path to the XML file.
/// </param>
/// <typeparam name="TSerializable">
/// The type of the serializable object.
/// </typeparam>
/// <returns>
/// An instance created from the XML input.
/// </returns>
public static TSerializable CreateFromXmlFile<TSerializable>(string filePath)
{
var serializer = new DataContractSerializer(typeof(TSerializable));
using (var reader = XmlReader.Create(filePath))
{
return (TSerializable)serializer.ReadObject(reader);
}
}
public static T LoadJson<T>(Stream stream) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
object readObject = serializer.ReadObject(stream);
return (T)readObject;
}
public static void WriteJson<T>(this T value, Stream stream) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
serializer.WriteObject(stream, value);
}