我有一个 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(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 接口(interface),这可能不是您的情况。
关于c# - 读取和处理 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2218031/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信