使用Windows应用程序将xml转换为UI [英] convert xml to UI using windows application

查看:113
本文介绍了使用Windows应用程序将xml转换为UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些值的xml配置文件.此xml应该通过控件转换为winform的前端.
例如,如果我有文本,则应在文本框中,在下拉列表中应包含枚举值.

解决方案

try
{
 //Insert the file in bin folder
                XmlDocument doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\XMLFileWindow.xml");
                XmlNodeList nodes = doc.SelectNodes("/Label");
                //Create Dynamic Assembly              
                Assembly myAssembly = null;
                myAssembly = Assembly.GetCallingAssembly();
                //Loading                
                foreach (XmlNode node in nodes)
                {
                    try
                    {
                        //Loading Control Class specific to Label
                        Type tControl = myAssembly.GetType("System.Windows.Forms." + node.Name);
                        object objControl = Activator.CreateInstance(tControl, null);
                        foreach (XmlAttribute at in node.Attributes)
                        {
                            //Get Property Info
                            PropertyInfo p = tControl.GetProperty(at.Name);
                            if (p.Name == "Location")
                            {
                                string[] chLocation = at.Value.Split('','');
                                p.SetValue(objControl, new Point(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
                            }
                            else if (p.Name == "Size")
                            {
                                string[] chLocation = at.Value.Split('','');
                                p.SetValue(objControl, new Size(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
                            }
                            else if (p.Name == "TextAlign")
                                p.SetValue(objControl, HorizontalAlignment.Center, null);
                            else
                                p.SetValue(objControl, at.Value, null);
                        }                        
                        this.Controls.Add((Control)objControl);
                    }
                    catch (Exception exx)
                    {
                        Console.WriteLine(exx.Message);
                        return;
                    }

如果有帮助,请祝你好运.


然后从此处开始 XElement [ ^ ]

在那里,您可以使用XElement类来加载XML文件并处理所有信息.

XElement xmlConfig = XElement.Load("Config.xml");

foreach (XElement elm in xmlConfig.Elements)
{
  Console.WriteLine("XML name:" + elm.Local.Name + ", Value:" + elm.Value);
}


I have a xml configuration file with certain values. This xml should be converted into front end on winform with controls.
For example if i have a text, it should be on textbox, enum values on dropdown etc.

解决方案

try
{
 //Insert the file in bin folder
                XmlDocument doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + "\\XMLFileWindow.xml");
                XmlNodeList nodes = doc.SelectNodes("/Label");
                //Create Dynamic Assembly              
                Assembly myAssembly = null;
                myAssembly = Assembly.GetCallingAssembly();
                //Loading                
                foreach (XmlNode node in nodes)
                {
                    try
                    {
                        //Loading Control Class specific to Label
                        Type tControl = myAssembly.GetType("System.Windows.Forms." + node.Name);
                        object objControl = Activator.CreateInstance(tControl, null);
                        foreach (XmlAttribute at in node.Attributes)
                        {
                            //Get Property Info
                            PropertyInfo p = tControl.GetProperty(at.Name);
                            if (p.Name == "Location")
                            {
                                string[] chLocation = at.Value.Split('','');
                                p.SetValue(objControl, new Point(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
                            }
                            else if (p.Name == "Size")
                            {
                                string[] chLocation = at.Value.Split('','');
                                p.SetValue(objControl, new Size(Convert.ToInt16(chLocation[0].ToString()), Convert.ToInt16(chLocation[1].ToString())), null);
                            }
                            else if (p.Name == "TextAlign")
                                p.SetValue(objControl, HorizontalAlignment.Center, null);
                            else
                                p.SetValue(objControl, at.Value, null);
                        }                        
                        this.Controls.Add((Control)objControl);
                    }
                    catch (Exception exx)
                    {
                        Console.WriteLine(exx.Message);
                        return;
                    }

Good luck if it is helpfull.


Then start here XElement[^]

There you can use XElement class to load in your XML file and process all informations.

XElement xmlConfig = XElement.Load("Config.xml");

foreach (XElement elm in xmlConfig.Elements)
{
  Console.WriteLine("XML name:" + elm.Local.Name + ", Value:" + elm.Value);
}


这篇关于使用Windows应用程序将xml转换为UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆