Restore data into List

  • Thread starter Thread starter DenisAndreevich
  • Start date Start date
D

DenisAndreevich

Guest
I have struct, that looks that:

public struct Notes
{
public string TitleOfNote
{
get;
set;
}

public string ContentsOfNote
{
get;
set;
}

public Notes(string titleOfNote, string ContentsOfNote)
{
this.TitleOfNote = titleOfNote;
this.ContentsOfNote = ContentsOfNote;
}
public override string ToString()
{
return $"You have entered: {TitleOfNote}\n" +
$"{ContentsOfNote}\n";
}


}

That struct fill next:

private void Action1()
{
string titleOfNote = null;
string ContentsOfNote = null;
bool correct = true;

do
{
correct = true;
WriteLine("Enter title of note:");
titleOfNote = ReadLine();

WriteLine("Enter note content:");
ContentsOfNote = ReadLine();

if (titleOfNote == null || titleOfNote == "")
{
WriteLine("Title of note should have one symbol." +
"One more time");
correct = false;
}
if (correct)
{
WorkWithFS workWithFS = new WorkWithFS();
workWithFS.WriteUserDatas(AddNotes(titleOfNote, ContentsOfNote));

}
} while (correct == false);

}

public List<Notes> AddNotes(string formalTitleOfNote, string formalContentsOfNote)
{
List<Notes> notes = new List<Notes>();

notes.Add(new Notes(formalTitleOfNote, formalContentsOfNote));

foreach (var item in notes)
{
WriteLine(item);
}

return notes;
}

On exit, StreamWriter create a file with data which was input and i have List with object which i can edit while this data on RAM. But, after load program StreamReader read data like regular console stream. I tried restore List like that:

public void ReadUserData()
{

using (StreamReader streamReader = new StreamReader("test.bta"))
{
string line = streamReader.ReadToEnd();
string[] ends = new string[] { "<NoteName>" };
string[] entered = new string[] { "<NoteContent>" };
string[] data = line.Split(ends, StringSplitOptions.None);
string[] notes = line.Split(entered, StringSplitOptions.None);

for (int i = 0; i < notes.Length; i++)
{
Write("{0}", data);
//Write("{0}", notes);
}
}
}

It is easy to guess, using this method I will have problems splitting the text and filling in the array

Palace Winter
Take Shelter<NoteContent>

oomph!
<NoteContent>Tausend Mann und ein Befehl


Rammstein
<NoteContent>Mein Herz brennt

So, my question is: How i can restore data from file as a List<Notes>?

Deeper question: how should I properly organize the structure of writing to the file of objects that are stored in the list?

Continue reading...
 
Back
Top