大家觉得有什么可以改进的地方,如果用反射的话,能不能给点思路附件地址http://115.com/file/bhgk10u9# XML解析.zip谢谢了~~~真心求指点,我是新手,主管给我出的题目,大家不用写的很具体,给我点提示就行

解决方案 »

  1.   

    这是要解析的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());
            }
        }
    }
      

  2.   

    忘了,还有一个
    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);
                }
            }
        }
    }
      

  3.   

    Convert.ToInt32 用  int.TryParse  代替之前说的问题,循环内的变量定义到循环外
      

  4.   

    好的,谢谢您了,我再改改大的方向还有什么建议么(比如说对xml的处理,以及类的设计)?
    您觉得用反射好么~~