可以使用BinaryFormatter。這對於想要一個小文件是一個很好的解決方案,但只有你知道它是否是你的域名的最佳解決方案。不過,我認爲你一次只能閱讀一條記錄。
我現在唯一的示例代碼是DataSet。這些擴展方法將(de)序列化一個自定義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簡而言之,阿爾巴哈利&阿爾巴哈利)。在這種情況下,您還需要閱讀Best Practices: Data Contract Versioning。下面是如何以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);
}
來源
2011-03-18 15:34:34
Pat
對於第二個選項:[FileDB](http://nuget.org/List/Packages/FileDB)。 – 2011-03-18 15:47:41
謝謝Martinho。我喜歡FileDB! – Aliostad 2011-03-18 16:01:22