Winform中C#读取xml动态创建控件

废话不多少,直接上代码

Form1.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>
复制代码

 

posted on   sduSRZ  阅读(981)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示