현재 이진 serializer를 사용하여 하나 또는 두 개의 기본 클래스에서 .txt 파일로 여러 속성을 읽고 쓰는 응용 프로그램이 있습니다.사람이 읽을 수있는 파일을 저장하는 방법
나는 메모장에 .txt 파일을 연 및이 응용 프로그램에 대한 형식화 된 것 같이 어쨌든 = 나를 위해, 내가 XML을 사용하는 들었어요
하지만 꽤 D
인간의 눈에 매우 읽을 수 없습니다 아니다 대부분의 검색 결과는 지나치게 복잡해 보입니다.저장하려고하는 데이터의 종류는 단순히 "Person.cs"클래스의 모음이며, 이름과 주소, 모든 개인 문자열이지만 속성이 있고 Serializable로 표시된 것입니다.
사람이 쉽게 읽을 수있는 방식으로 실제로 데이터를 저장하는 가장 좋은 방법은 무엇입니까? 또한 응용 프로그램의 데이터를로드하고 변경하고 저장하는 대신 파일에서 직접 작은 변경을하는 것이 더 쉽습니다.
편집 :
내가, 내가 저장하고 내 데이터를로드하고 현재의 방법을 추가 한이 제안하고 nUSER 님/nMember는 정수로 내 _userCollection입니다.
#region I/O Operations
public bool SaveData()
{
try
{
//Open the stream using the Data.txt file
using (Stream stream = File.Open("Data.txt", FileMode.Create))
{
//Create a new formatter
BinaryFormatter bin = new BinaryFormatter();
//Copy data in collection to the file specified earlier
bin.Serialize(stream, _userCollection);
bin.Serialize(stream, nMember);
bin.Serialize(stream, nUser);
//Close stream to release any resources used
stream.Close();
}
return true;
}
catch (IOException ex)
{
throw new ArgumentException(ex.ToString());
}
}
public bool LoadData()
{
//Check if file exsists, otherwise skip
if (File.Exists("Data.txt"))
{
try
{
using (Stream stream = File.Open("Data.txt", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
//Copy data back into collection fields
_userCollection = (List<User>)bin.Deserialize(stream);
nMember = (int)bin.Deserialize(stream);
nUser = (int)bin.Deserialize(stream);
stream.Close();
//Sort data to ensure it is ordered correctly after being loaded
_userCollection.Sort();
return true;
}
}
catch (IOException ex)
{
throw new ArgumentException(ex.ToString());
}
}
else
{
//Console.WriteLine present for testing purposes
Console.WriteLine("\nLoad failed, Data.txt not found");
return false;
}
}
XmlSerializer에는 BinaryFormatter와 비교하여 여러 가지 제한 사항이 있습니다. 예를 들어 개인 데이터를 직렬화 할 수 없습니다. –
DataContractSerializer는 재미 있지만, 개인적으로 XMLSerializer의 사용법은 성격과 특성이 동일하지 않기 때문에 BinaryFormatter에 더 가깝다고 생각합니다. –
토마스는 사실에 더 가깝다고 생각합니다 ... 특히 서비스 팩의 경우 비 속성 유형에 대한 DCS의 기본 동작은 BF와 매우 유사합니다. 물론 BF도 아닌 DCS도 좋은 선택입니다. 바로 이런 이유로! –