c#解析带命名空间的xml,求优化 大家觉得有什么可以改进的地方,如果用反射的话,能不能给点思路附件地址http://115.com/file/bhgk10u9# XML解析.zip谢谢了~~~真心求指点,我是新手,主管给我出的题目,大家不用写的很具体,给我点提示就行 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是要解析的xml代码,就是解析里面的信息,把它作为要添加的按钮的描述<?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()); } }} 忘了,还有一个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); } } }} Convert.ToInt32 用 int.TryParse 代替之前说的问题,循环内的变量定义到循环外 好的,谢谢您了,我再改改大的方向还有什么建议么(比如说对xml的处理,以及类的设计)?您觉得用反射好么~~ 有个dgv的问题,请高手指点! 开源的绘图软件 C#与现场总线 我要建一个类的实例10个,请问有没有什么数组可以放下这10个实例的? 一个成员函数前加上这么[Serializable]一句,是什么意思? 在picturebox上画图,然后按了最小化,却出现了错误 请教C#关于大批量数据的循环效率 正则匹配问题。求助 C# 用USB连接设备,接收和发送数据,请大侠们指教一下!谢谢! ArrayList如何动态控制一个二维数组 怎么把数据库的内容显示在DataGridView中 在HTTPS下,如何用 POST 实现传输 XML ?
<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());
}
}
}
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);
}
}
}
}
您觉得用反射好么~~