|
일일 결과물 | |||
이 름 |
블라디미르 |
일 시 |
2010-10-21 |
소요시간 |
3 시간 |
3 시간 |
|
주 제 명 |
Save and Load data and settings Parsing a structured text file and extracting its data. |
Research Overview
The purpose of this research was for me to become familiar with parsing structured text file and extract or load its data because during the deployment of our project we will deal with this kind of situations while saving and opening the results or definitions of the web content evaluations to make it easy for the users to make various evaluations with the same options and or share evaluations results and or options with other users.
Form Interface
The data structure inside of the program
this is basically the data structure of the program
public class Person { public StringDictionary p_Data = new StringDictionary();
public string this[string _Info] { get { return p_Data[_Info]; } set { p_Data[_Info] = value; } } public Person() { } } |
The parser inside of the program
This is a simple parser that should work with the data provided in our project but may not work for all fringe conditions. This parser will also work for any data in the same format (new line separated info value pairs delimited by spaces and values delimited by an empty line) building up file records of data.
class FileParser { public ArrayList ParseStream(Stream _Str) { ArrayList ret = new ArrayList(); Person Current = new Person();
if (!_Str.CanRead) { throw new System.ArgumentException("Non_Readable stream passed"); }
StreamReader sr = new StreamReader(_Str);
while (sr.Peek() >= 0) { string CurrentLine = sr.ReadLine();
if (CurrentLine.Trim() == "") { ret.Add(Current); Current = new Person(); } else { string[] Data = CurrentLine.Split(':');
if (Data.Length == 2) { string Info = Data[0].Trim(); string value = Data[1].Trim();
Current[Info] = value; } } } ret.Add(Current);
return ret; } } |
The OK button.
When we hit the OK button, the data in the textBoxes and the status of the checkBoxes is passed in to a listBox(that is not necessary to show up and be hided working in the background), that in this case works as a volatile dataBase keeping all the data while the program is running.
private void button1_Click(object sender, EventArgs e) { string Name = textBox1.Text; string Gender = textBox2.Text; string Address = textBox3.Text;
ListBox.ObjectCollection items = listBox1.Items; items.Add("Name: " + textBox1.Text); items.Add("Gender: " + textBox2.Text); items.Add("Address:" + textBox3.Text);
if (checkBox1.Checked == true) { listBox1.Items.Add("KindStatus: " + true); } else { listBox1.Items.Add("KindStatus: " + false); } if (checkBox2.Checked == true) { listBox1.Items.Add("FriendStatus: " + true); } else { listBox1.Items.Add("FriendStatus: " + false); } if (checkBox3.Checked == true) { listBox1.Items.Add("StrongStatus: " + true); } else { listBox1.Items.Add("StrongStatus: " + false); } textBox1.Clear(); textBox2.Clear(); textBox3.Clear();
} |
The SAVE button
When we click the save button all the item in the listBox is saved in a text file(that could be any other type of file according to the needs).
private void Savebutton_Click(object sender, EventArgs e) { SaveFileDialog sfdlg = new SaveFileDialog();
sfdlg.DefaultExt = ".txt"; sfdlg.Filter = "txt File(.txt) | *.txt";
if (sfdlg.ShowDialog() == DialogResult.OK) { if (sfdlg.FileName != null) { System.IO.StreamWriter sw = new System.IO.StreamWriter(sfdlg.FileName);
foreach (object item in listBox1.Items) sw.WriteLine(item.ToString()); sw.Close(); } } } |
The OPEN button
When we click the OPEN button after select the desired file it firs clears all the data and settings in all the form fields and then places the values according to its info.
private void Open_Click(object sender, EventArgs e) { OpenFileDialog ofdlg = new OpenFileDialog();
ofdlg.DefaultExt = ".txt"; ofdlg.Filter = "txt File(.txt) | *.txt";
if (ofdlg.ShowDialog() == DialogResult.OK) { textBox1.Clear(); textBox2.Clear(); textBox3.Clear();
FileStream fs = File.Open(ofdlg.FileName, FileMode.Open); try { FileParser Parser = new FileParser(); ArrayList records = Parser.ParseStream(fs);
ListBox.ObjectCollection items = listBox1.Items; foreach (Person record in records) { textBox1.AppendText(record["Name"]); textBox2.AppendText(record["Gender"]); textBox3.AppendText(record["Address"]);
if (Convert.ToBoolean(record["KindStatus"]) == true) { checkBox1.Checked = true; } else { checkBox1.Checked = false; } if (Convert.ToBoolean(record["FriendStatus"]) == true) { checkBox2.Checked = true; } else { checkBox2.Checked = false; } if (Convert.ToBoolean(record["StrongStatus"]) == true) { checkBox3.Checked = true; } else { checkBox3.Checked = false; } } } finally { fs.Close(); } } } |
The final result
It gets back to the first status after save and load data.
Future work : the next step will be the adaptation of this parsing method in to the project where it will have to handle about hundred checkBoxs and many textboxes and or maybe some other things and many different kinds of data. .,
2010-10-21-일일결과물-Parse a structured text file an.docx
|