我有一个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();
}
}
}
}
...上面的代码生成零个误差。但是,它不起作用。表单上没有显示任何控件。有人知道为什么上面的代码不起作用,我怎么才能使它工作?
所有帮助表示赞赏,谢谢
巴尔
当您通过代码进行调试时会发生什么? – 2010-02-07 20:42:57