당신은 BinaryFormatter를 사용할 수 있습니다. 작은 파일을 원한다면 좋은 솔루션이지만 도메인에 가장 적합한 솔루션인지는 알 수 있습니다. 그래도 한 번에 하나의 레코드를 읽을 수 있다고 생각하지 않습니다.
이 예제 코드는 DataSet입니다. 이러한 확장 메서드는 사용자 지정 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);
}
당신은 또한 (간단히 말해서 C# 4.0, Albahari & Albahari에 따라) 직렬화를 처리하는 .NET의 새로운 '표준'방식이다 DataContractSerializer 한 번 봐, 걸릴 수 있습니다. 이 경우 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)입니다. –
Martinho 감사합니다. 나는 FileDB를 좋아했다! – Aliostad