标签:c linq xml winforms linq-to-xml
我有一个Xml文件.它的格式如下:
ControlType > Content > LocationX > LocationY > ForeColor/LinkColor > Int > Int > Int > Int
文件示例:
<?xml version="1.0" encoding="utf-8"?>
<cs>
<Label Content="Double-click to edit." LocationX="583" LocationY="254" A="255" R="255" G="255" B="255" />
<LinkLabel Content="Double-click to edit." LocationX="613" LocationY="251" A="255" R="0" G="0" B="0" />
</cs>
背景:生成Xml文件并将其保存在磁盘上.当用户将Xml文档加载到我的应用程序中时,我的应用程序将读取Xml文件.并且在文档中对foreach进行控制,它将检索其属性,如下所示:
foreach(Control control in XmlFile)
{
// get control type
// get control content
// get LocationX
// get LocationY
// get Color
// get Int
// get Int
// get Int
// get Int
// Do something with retrieved data.
}
这是我已经拥有的:
OpenFileDialog o = new OpenFileDialog();
o.Filter =
"T Multimedia Format (*.mf)|*.mf|" +
"Word Document (*.docx)|*.docx|" +
"PDF Document (*.pdf)|*.pdf|" +
"Text FIle (*.txt)|*.txt";
o.Title = "T 11 - Open Document";
using (o)
{
if (o.ShowDialog() == DialogResult.OK)
{
XDocument xdc = XDocument.Load(o.FileName);
var cs = xdc.Elements("cs");
foreach (var im in cs)
{
if (im.Name == "Label")
{
Label label = new Label();
label.MouseClick += new MouseEventHandler(label_MouseClick);
label.MouseDown += new MouseEventHandler(label_MouseDown);
label.MouseMove += new MouseEventHandler(label_MouseMove);
label.MouseUp += new MouseEventHandler(label_MouseUp);
label.MouseDoubleClick += new MouseEventHandler(label_MouseDoubleClick);
label.Text = im.Attribute("Content").Value;
label.Location = new Point(
Convert.ToInt32(im.Attribute("LocationX").Value),
Convert.ToInt32(im.Attribute("LocationY").Value));
label.BackColor = Color.Transparent;
label.ForeColor = Color.FromArgb(
Convert.ToInt32(im.Attribute("A").Value),
Convert.ToInt32(im.Attribute("R").Value),
Convert.ToInt32(im.Attribute("G").Value),
Convert.ToInt32(im.Attribute("B").Value));
label.AutoSize = true;
Canvas.Controls.Add(label);
label.BringToFront();
}
else if (im.Name == "LinkLabel")
{
LinkLabel link = new LinkLabel();
link.MouseClick += new MouseEventHandler(link_MouseClick);
link.MouseDown += new MouseEventHandler(link_MouseDown);
link.MouseMove += new MouseEventHandler(link_MouseMove);
link.MouseUp += new MouseEventHandler(link_MouseUp);
link.MouseDoubleClick += new MouseEventHandler(link_MouseDoubleClick);
link.Text = im.Attribute("Content").Value;
link.Location = new Point(
Convert.ToInt32(im.Attribute("LocationX").Value),
Convert.ToInt32(im.Attribute("LocationY").Value));
link.BackColor = Color.Transparent;
link.LinkColor = Color.FromArgb(
Convert.ToInt32(im.Attribute("A").Value),
Convert.ToInt32(im.Attribute("R").Value),
Convert.ToInt32(im.Attribute("G").Value),
Convert.ToInt32(im.Attribute("B").Value));
link.AutoSize = true;
Canvas.Controls.Add(link);
link.BringToFront();
}
}
}
}
…上面的代码生成零错误.但是,它只是不起作用.表单上没有显示任何控件.有谁知道为什么上面的代码不起作用,以及我如何使它工作?
感谢所有帮助,谢谢
巴尔
解决方法:
我会建议你更通用的方式:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var controlTag in XDocument.Load("settings.xml").Root.Elements())
{
var controlType = Type.GetType(string.Format("System.Windows.Forms.{0}, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", controlTag.Name.LocalName), false);
if (controlType == null || !typeof(Control).IsAssignableFrom(controlType))
{
continue;
}
var control = (Control)Activator.CreateInstance(controlType);
control.Text = controlTag.Attribute("Content").Value;
control.Location = new Point(
int.Parse(controlTag.Attribute("LocationX").Value),
int.Parse(controlTag.Attribute("LocationY").Value)
);
control.BackColor = Color.Transparent;
control.MouseClick += mouseClick;
control.MouseDown += mouseDown;
control.MouseMove += mouseMove;
control.MouseUp += mouseUp;
control.MouseDoubleClick += mouseDoubleClick;
Controls.Add(control);
}
}
至于颜色,您可以向xml文件添加一个属性,该属性将指向我们要设置的控件的属性名称并使用反射.例如:
<?xml version="1.0" encoding="utf-8"?>
<cs>
<Label Content="Double-click to edit." LocationX="583" LocationY="254" A="255" R="255" G="255" B="255" ColorProperty="ForeColor" />
<LinkLabel Content="Double-click to edit." LocationX="613" LocationY="251" A="255" R="0" G="0" B="0" ColorProperty="LinkColor" />
</cs>
顺便提一下,XAML已经提供了许多你想要在这里实现的功能.当然它假设WPF接口可能不是你的情况.
标签:c,linq,xml,winforms,linq-to-xml 来源: https://codeday.me/bug/20190701/1343607.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。