c#解析带命名空间的xml,求优化

eodsafma 2011-12-01 05:29:16
大家觉得有什么可以改进的地方,如果用反射的话,能不能给点思路

附件地址http://115.com/file/bhgk10u9# XML解析.zip

谢谢了~~~真心求指点,我是新手,主管给我出的题目,大家不用写的很具体,给我点提示就行
...全文
265 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
eodsafma 2011-12-02
  • 打赏
  • 举报
回复
好的,我了解下~~,谢谢您的建议[Quote=引用 14 楼 sdl2005lyx 的回复:]

建议考虑xml反序列化。。。
[/Quote]
sdl2005lyx 2011-12-02
  • 打赏
  • 举报
回复
建议考虑xml反序列化。。。
Daqing 2011-12-01
  • 打赏
  • 举报
回复
太长了,订一腿吧
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sandy945 的回复:]

你现在的写法针对性较强,如果结构有变动的话,就没办法重用了。

应用反射的话 性能会有所损失,但会更灵活,从解析结构的角度。
[/Quote]

不用太考虑这个写法,性能相对来说也不是主要的,我现在就是想把它弄得扩展性、适应性强点,
涉及到的常用的技术越多越好……

我先用反射来试试
阿非 2011-12-01
  • 打赏
  • 举报
回复
你现在的写法针对性较强,如果结构有变动的话,就没办法重用了。

应用反射的话 性能会有所损失,但会更灵活,从解析结构的角度。
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sandy945 的回复:]

Convert.ToInt32 用 int.TryParse 代替

之前说的问题,循环内的变量定义到循环外
[/Quote]
好的,谢谢您了,我再改改

大的方向还有什么建议么(比如说对xml的处理,以及类的设计)?
您觉得用反射好么~~
阿非 2011-12-01
  • 打赏
  • 举报
回复
Convert.ToInt32 用 int.TryParse 代替

之前说的问题,循环内的变量定义到循环外
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sandy945 的回复:]

现在的有什么问题么?

写的还可以,循环中的一些变量可以放到循环外定义,集合如果知道长度的话,就在初始化的同时指定长度
[/Quote]
问题是没有,我刚实习,主管想通过这个例子来看看我的水平。
所以让我写的好一点,效率高点,
想让大家给提点意见,看什么地方可以优化一下,
或者怎么样才能更专业点
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
忘了,还有一个
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);
}
}
}
}
阿非 2011-12-01
  • 打赏
  • 举报
回复
现在的有什么问题么?

写的还可以,循环中的一些变量可以放到循环外定义,集合如果知道长度的话,就在初始化的同时指定长度
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhs23 的回复:]

写的很牛逼了
[/Quote]水平真的很有限,啥也不熟~~就像求大家给个思路,我在上网搜搜~~
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sandy945 的回复:]

你还是把主要代码贴出来比较好
[/Quote]

已经贴出来了,麻烦您看看~~
eodsafma 2011-12-01
  • 打赏
  • 举报
回复
这是要解析的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());
}
}
}
迷迷520 2011-12-01
  • 打赏
  • 举报
回复
写的很牛逼了
阿非 2011-12-01
  • 打赏
  • 举报
回复
你还是把主要代码贴出来比较好

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧