111,120
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace XML解析
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateMedia();
}
void CreateMedia()
{
XmlParser parser = new XmlParser();
List<Dictionary<String, String>> btns = parser.BtnComponents;
List<Dictionary<String, String>> labels = parser.LabelComponents;
//var btns = parser.BtnComponents;
//var labels = parser.LabelComponents;
foreach (var item in btns)
{
int _x = 0;
int _y = 0;
int _width = 0;
int _height = 0;
String _label = String.Empty;
foreach (var key in item.Keys)
{
switch (key)
{
case "x":
_x = Convert.ToInt32(item[key]);
break;
case "y":
_y = Convert.ToInt32(item[key]);
break;
case "width":
_width = Convert.ToInt32(item[key]);
break;
case "height":
_height = Convert.ToInt32(item[key]);
break;
case "label":
_label = item[key];
break;
}
}
Button btn = new Button();
btn.Location = new System.Drawing.Point(_x, _y);
btn.Size = new System.Drawing.Size(_width, _height);
btn.Text = _label;
this.Controls.Add(btn);
}
foreach (var item in labels)
{
int _x = 0;
int _y = 0;
int _width = 0;
int _height = 0;
String _text = String.Empty;
foreach (var key in item.Keys)
{
switch (key)
{
case "x":
_x = Convert.ToInt32(item[key]);
break;
case "y":
_y = Convert.ToInt32(item[key]);
break;
case "width":
_width = Convert.ToInt32(item[key]);
break;
case "height":
_height = Convert.ToInt32(item[key]);
break;
case "text":
_text = item[key];
break;
}
}
Label label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(_x, _y);
label.Size = new System.Drawing.Size(_width, _height);
label.TabIndex = 0;
label.Text = _text;
this.Controls.Add(label);
}
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Component
xmlns="http://www.lizhenkai.com"
xmlns:jingyi1="http://www.jingyi.com/generation1"
xmlns:jingyi2="http://www.jingyi.com/generation2">
<jingyi1:Buttons>
<Button x="10" y="15" width="50" height="20" label="按钮1"></Button>
<Button x="30" y="55" width="60" height="20" label="按钮2"></Button>
</jingyi1:Buttons>
<jingyi2:Labels>
<Label x="170" y="130" width="80" height="20" text="这是标签1"></Label>
<Label x="180" y="170" width="80" height="20" text="这是标签2"></Label>
</jingyi2:Labels>
</Component>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XML解析
{
class XmlParser
{
private List<Dictionary<String, String>> _buttonComponents;
private List<Dictionary<String, String>> _labelComponents;
public XmlParser()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"component.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("author", "http://www.lizhenkai.com");
nsmgr.AddNamespace("jingyi1", "http://www.jingyi.com/generation1");
nsmgr.AddNamespace("jingyi2", "http://www.jingyi.com/generation2");
int i;
if (doc.SelectSingleNode("author:Component/jingyi1:Buttons", nsmgr).HasChildNodes)
{
XmlNodeList btnNodes = doc.SelectSingleNode("author:Component/jingyi1:Buttons", nsmgr).ChildNodes;
_buttonComponents = new List<Dictionary<String, String>>(btnNodes.Count);
for (i = 0; i < btnNodes.Count; i++)
{
String _x = btnNodes[i].Attributes["x"].Value;
String _y = btnNodes[i].Attributes["y"].Value;
String _width = btnNodes[i].Attributes["width"].Value;
String _height = btnNodes[i].Attributes["height"].Value;
String _label = btnNodes[i].Attributes["label"].Value;
Dictionary<String, String> btnComponent = new Dictionary<string, String>();
btnComponent.Add("x", _x);
btnComponent.Add("y", _y);
btnComponent.Add("width", _width);
btnComponent.Add("height", _height);
btnComponent.Add("label", _label);
_buttonComponents.Add(btnComponent);
}
}
if (doc.SelectSingleNode("author:Component/jingyi2:Labels", nsmgr).HasChildNodes)
{
XmlNodeList labelNodes = doc.SelectSingleNode("author:Component/jingyi2:Labels", nsmgr).ChildNodes;
_labelComponents = new List<Dictionary<String, String>>(labelNodes.Count);
for (i = 0; i < labelNodes.Count; i++)
{
String _x = labelNodes[i].Attributes["x"].Value;
String _y = labelNodes[i].Attributes["y"].Value;
String _width = labelNodes[i].Attributes["width"].Value;
String _height = labelNodes[i].Attributes["height"].Value;
String _text = labelNodes[i].Attributes["text"].Value;
Dictionary<String, String> labelComponent = new Dictionary<string, String>();
labelComponent.Add("x", _x);
labelComponent.Add("y", _y);
labelComponent.Add("width", _width);
labelComponent.Add("height", _height);
labelComponent.Add("text", _text);
_labelComponents.Add(labelComponent);
}
}
}
public List<Dictionary<String, String>> BtnComponents
{
get { return _buttonComponents; }
}
public List<Dictionary<String, String>> LabelComponents
{
get { return _labelComponents; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace XML解析
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}