c# 读取xml文件 编写form
主要思想:xml保存控件的数据,c#读取出来并加以显示。
难点:1.控件有父容器和子控件的关系;2.控件事件的添加。
1.控件有父容器和子控件的关系:
可以用绝对坐标在xml文件中先读取子控件再读取父容器;
也可以在子控件属性中设置一个标签记录父容器,然后c#读取时候父容器add子控件。
2.控件事件的添加:
在控件add完之后也可以对事件进行添加(包括属性更改)。
为了精确确定某个控件,c#读取到的诸多控件,不建议采用数组包装,用dictionnary省力。
代码:
xml文件:
<?xml version='1.0' encoding='UTF-8'?> <XmlDataForm Name="ReportSum" Text="报告模板总" ClientSize="1086,1058" DataEntity=""> <Controls> <Button name="button_firstLang" Location="51,32" Size="38,28" Text="CN" /> <Button name="button_secondLang" Location="96,32" Size="38,28" Text="EN" /> <GroupBox name="groupBox2" Location="188,12" Size="854,58" Text="采图信息" /> <Label name="label1" Location="68,24" Size="32,17" Text="姓名" /> <TextBox name="textBox_name" Location="103,21" Size="120,23" Text="" /> <Label name="label2" Location="229,24" Size="32,17" Text="性别" /> <ComboBox name="comboBox_sex" Location="263,21" Size="45,25" Text="" /> <Label name="label3" Location="313,24" Size="32,17" Text="年龄" /> <TextBox name="textBox_age" Location="348,21" Size="28,23" Text="" /> <Label name="label5" Location="417,24" Size="56,17" Text="采图站点" /> <TextBox name="textBox_site" Location="476,21" Size="92,23" Text="" /> <Label name="label6" Location="572,24" Size="60,17" Text="" /> <TextBox name="textBox_create_date" Location="635,21" Size="137,23" Text="" /> </Controls> </XmlDataForm>
c#:
public void xmlLoad()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"moban1.xml");
XmlNodeList list_body = xmlDoc.SelectNodes("XmlDataForm");
getControlFromXml(list_body[0]);
this.Width = int.Parse(formWeight);
this.Height = int.Parse(formHeight);
this.Text = formTitle;
foreach (var itemControl in controlsFromXml)
{
Control control = itemControl.Value;
if (control == null) { continue; }
bool addCtrBool = true;
if (control.Tag != null)
{
foreach (var cto in controlsFromXml)
{
Control tempControl = cto.Value;
if (tempControl == null)
{
continue;
}
if (tempControl.Name.Equals(control.Tag.ToString()))
{
tempControl.Controls.Add(control);
addCtrBool = false;
break;
}
}
}
if (addCtrBool)
{
this.Controls.Add(control);
}
}
}
string formWeight; string formHeight; string formTitle; Dictionary<string, Control> controlsFromXml; public void getControlFromXml(XmlNode xmlNode) { formWeight=xmlNode.Attributes["ClientSize"].Value.Split(',')[0]; formHeight=xmlNode.Attributes["ClientSize"].Value.Split(',')[1]; formTitle=xmlNode.Attributes["Text"].Value; XmlNodeList list=xmlNode.SelectSingleNode("Controls").ChildNodes; controlsFromXml = new Dictionary<string, Control>(); for (int i = 0; i < list.Count; i++) { XmlNode tempXmlNode = list[i]; string className = tempXmlNode.Name; Assembly assembly = Assembly.LoadFrom(@"System.Windows.Forms.dll"); if (className != null&&!className.Contains("#")&&className!="") { Type type = assembly.GetType(@"System.Windows.Forms."+className); object obj = Activator.CreateInstance(type); Control control = (Control)obj; control.Left = int.Parse(tempXmlNode.Attributes["Location"].Value.Split(',')[0]); control.Top = int.Parse(tempXmlNode.Attributes["Location"].Value.Split(',')[1]); control.Width = int.Parse(tempXmlNode.Attributes["Size"].Value.Split(',')[0]); control.Height = int.Parse(tempXmlNode.Attributes["Size"].Value.Split(',')[1]); control.Name = tempXmlNode.Attributes["Name"].Value; if (tempXmlNode.Attributes["Father"] != null) { control.Tag = tempXmlNode.Attributes["Father"].Value; } if (tempXmlNode.Attributes["Text"]!= null) { control.Text = tempXmlNode.Attributes["Text"].Value; } controlsFromXml.Add(control.Name,control); } } }

浙公网安备 33010602011771号