• 读取xml动态创建控件


    from1.cs:

    public partial class Form1 : Form
    {
        public Form1()
        {
            int x = 0;
            int y = 0;
            XmlDocument Xd = new XmlDocument();
            Xd.Load("D:\config.xml");
    
            XmlNode testNode = Xd.SelectSingleNode("Test");
            XmlNodeList list = testNode.SelectNodes("template");
    
            foreach (XmlNode xn in list)
            {
                string name = xn.SelectSingleNode("name").InnerText.Trim();
    
                Button button = new Button
                {
                    Text = name,
                    Width = 250,
                    Height = 75,
                    Left = x + 20,
                    Top = y,
                };
                button.Click += new EventHandler(Button_Click);
                Controls.Add(button);
                y += button.Height + 5;
            }
            InitializeComponent();
        }
    
        private void Button_Click(object sender, EventArgs e)
        {
            Form4 form = new Form4();
            Button b = sender as Button;
            string name = b.Text;
            Create(form, name);
            form.ShowDialog();
        }
    
        private void Create(Form form, string name)
        {
            int x = 0;
            int y = 0;
            XmlDocument Xd = new XmlDocument();
            Xd.Load("D:\config.xml");
    
            XmlNode testNode = Xd.SelectSingleNode("Test");
            XmlNodeList list = testNode.SelectNodes("template");
    
            foreach (XmlNode node in list)
            {
                foreach (XmlNode xnode in node.ChildNodes)
                {
                    if (xnode.InnerText == name)
                    {
                        XmlNode newnode = xnode.ParentNode;
                        foreach (XmlNode itemnode in newnode.ChildNodes)
                        {
                            if (itemnode.Name == "item")
                            {
                                Label label = new Label
                                {
                                    Text = itemnode.InnerText,
                                    Width = 250,
                                    Height = 75,
                                    Left = x + 20,
                                    Top = y,
                                };
                                form.Controls.Add(label);
                                y += label.Height + 5;
                            }
                        }
                    }
                }
            }
        }
    }

    config.xml:

    <Test>
        <template id="Some Template ID">
              <name>Template name</name>
                  <description>Discription of this template</description>
                  <item id="1">1st item of this template</item>
                  <item id="2">2nd item of this template</item>
                  <item id="3">3rd item of this template</item>
                  <item id="4">4th item of this template</item>
        </template>
    
        <template id="Another Template ID">
              <name>Another template name</name>
              <description>Discription of this template</description>
              <item id="1">1st item of another  template</item>
              <item id="2">2nd item of another template</item>
              <item id="3">3rd item of another template</item>
              <item id="4">4th item of another template</item>
        </template>
    </Test>

    Result:

  • 相关阅读:
    SCRUM第一天
    第八周总结
    第7周总结
    团队项目nabcd
    人月神话1
    课堂练习之四则运算
    第六周总结
    Storm Grouping —— 流分组策略
    抓取网页内容生成Kindle电子书(转)
    浅析PageRank算法(转)
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/10413126.html
Copyright © 2020-2023  润新知