/// <summary> /// 將控制項序列化 /// </summary> /// <param name="Field">FormField的實例,用來儲存控制項內容與名稱</param> /// <param name="Ctrl">控制項</param> private void serializeControl(FormField Field, Control Ctrl) { foreach (Control ctrl in Ctrl.Controls) { FormField field = new FormField(ctrl); Field.childCtrls.Add(field); if (ctrl.Controls.Count > 0) { serializeControl(field, ctrl); } } }
/// <summary> /// 一個FormField實例儲存控制項的名稱與內容 /// 再利用XmlSerializer將這些內容輸出 /// </summary> /// <param name="File">欲輸出的檔案位置</param> /// <param name="root">控制項</param> public void Serialize(string File, Control root) { FormField field = new FormField(root); serializeControl(field, root); XmlSerializer xml = new XmlSerializer(typeof(FormField)); TextWriter writer = new StreamWriter(File); try { xml.Serialize(writer, field); } finally { writer.Close(); } }
public void Serialize(string File, List<Control> root) { List<FormField> fields = new List<FormField>(); foreach (Control ctrl in root) { FormField field = new FormField(ctrl); //Field.childCtrls.Add(field); serializeControl(field, ctrl); fields.Add(field); } XmlSerializer xml = new XmlSerializer(typeof(List<FormField>)); TextWriter writer = new StreamWriter(File); try { xml.Serialize(writer, fields); } finally { writer.Close(); } }
/// <summary> /// FormField裡有一串列儲存所有控制項內容與名稱 /// 此方法對串列中所有的控制項解譯並賦值(Text, Name) /// </summary> /// <param name="Field">FormField的實例,用來儲存控制項內容與名稱</param> /// <param name="Ctrl">控制項</param> private void DeserializeControl(FormField Field, Control Ctrl) { if (Field == null) return; foreach (FormField field in Field.childCtrls) { if (Ctrl.Controls[field.name] != null) { Ctrl.Controls[field.name].Text = field.text; if (field.childCtrls.Count > 0) { DeserializeControl(field, Ctrl.Controls[field.name]); } } } }