Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 25-Apr-11 17:04pm
v2
Comments
Sandeep Mewara 25-Apr-11 7:48am    
So what next? What is the issue that you are facing?

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.
 
Share this answer
 
v2
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);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900