.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> XML >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Dynamic Property with XML

Posted By:Narayanan       Posted Date: May 25, 2011    Points: 25    Category: XML    URL: http://www.dotnetspark.com  

In this article ,you get some idea about How to create Dynamic properties in C#.net and How to use these Properties in C#.net and to store a data to XML and How to retrieve Data from XML to Dynamic Property.
 

 Hi friends,
    This is my first Resource in this site. Any queries please send my ID naraayanan1983@gmail.com.I need valuable comments from you. Because it encourages me .OK, now I come to my Article. Last few days back I get this task. I am beginner in C#.net and XML.In this article ,you get some idea about  How to create Dynamic properties in C#.net and How to use these Properties in C#.net and to store a data to XML and How to retrieve Data from XML to Dynamic Property.
Step: 1
In First step, we will discuss about How to create Dynamic Property in C#.net.
Properties are GroupBox, ComboBox, TextBox and CheckBox.
Here these are the Declaration part of Dynamic Properties.
Public static System.Windows.Forms.GroupBox gb;
Public static System.Windows.Forms.TextBox txt;
Public static System.Windows.Forms.RadioButton chk;
Public static System.Windows.Forms.ComboBox cmb;
Here these are the Declaration String
Public static string txt1, txt2;
Public static string txt3;
Public static string text;
Here this is the Declaration String - To store a XML Path
Public static string ModifiedPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\Folder\\dataText";
 I have 3 buttons in my Form (Buttons are not created dynamically).Drag from Property window.
Button1 (Text) - - Property
Button2 (Text) - - Save
Button3 (Text) - - Display.
In First button click event:

private void button1_Click(object sender, EventArgs e)

        {

            gb = new GroupBox();

            gb.Name = "GroupName";

            gb.Text = "Virtual COM Port";

            gb.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            gb.Width = 750;

            gb.Height = 150;

            gb.Left = 10;

            gb.Top = 15;

            this.Controls.Add(gb);

            int i;

            for (i = 0; i < 3; i++)

            {

 

                cmb = new ComboBox();

                cmb.Name = "combobox" + i.ToString();

                cmb.Text ="Combo"+i.ToString();

                cmb.Width = 100;

                cmb.Height = 20;

                cmb.Width = 75;

                cmb.Height = 20;

                cmb.Left = 15;

                cmb.Top = 20 + (cmb.Height * i) + (10 * i); //initial + height + padding

                cmb.DropDownStyle = ComboBoxStyle.DropDownList;

                cmb.Items.Add("COM5");

                cmb.Items.Add("COM6");

 

              

                txt = new TextBox();

                txt.Name = "Textbox" + i.ToString();

                txt.Text = "Testtext" + i.ToString();

                txt.Width = 150;

                txt.Height = 20;

                txt.Left = 150;

                txt.Top = 20 + (txt.Height * i) + (10 * i);

 

                chk = new RadioButton();

                chk.Name = "RB" + i.ToString();

                chk.Text = "Enabled";

                chk.Width = 100;

                chk.Height = 20;

                chk.Left = 320;

                chk.Top = 20 + (chk.Height * i) + (10 * i); //initial + height + padding

 

                gb.Controls.Add(cmb);

                gb.Controls.Add(txt);

                gb.Controls.Add(chk);

 

 

            }

        }

// Create XML:

private static void createxml(String xmlpath)

        {

            int index;

            string sFilename = xmlpath;

            XmlDocument xmldoc = new XmlDocument();

            XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null);

            xmldoc.AppendChild(xmldec);

            XmlElement xmlele = xmldoc.CreateElement("Root");

            xmldoc.AppendChild(xmlele);

            for (index = 0; index < 3; index++)

            {

                string targetComboBox = "Combobox" + index;

                //Try to find the textbox

                int ComboBoxIndex = gb.Controls.IndexOfKey(targetComboBox);

                if (ComboBoxIndex != -1)

                {

                    ComboBox foundComboBox = (ComboBox)gb.Controls[ComboBoxIndex];

                    Userprefrenece.txt2 = foundComboBox.Text;

                }

 

                string targetTextBox = "Textbox" + index;

                //Try to find the textbox

                int textBoxIndex = gb.Controls.IndexOfKey(targetTextBox);

 

                if (textBoxIndex != -1)
                {

                    TextBox foundTextBox = (TextBox)gb.Controls[textBoxIndex];

                    Userprefrenece.txt1 = foundTextBox.Text;

                }

                string targetCheckBox = "RB" + index;

                //Try to find the textbox

                int targetCheckBoxs = gb.Controls.IndexOfKey(targetCheckBox);

                if (targetCheckBoxs != -1)
                {

                    RadioButton  foundCheckBox = (RadioButton)gb.Controls[targetCheckBoxs];

                    if (foundCheckBox.Checked == true)
                    {

                        Userprefrenece.txt3 = true;

                    }
                    else
                    {
                        Userprefrenece.txt3 = false ;
                    }

                }

                String str = "port" + index.ToString();

                XmlElement port = xmldoc.CreateElement(str);

                xmlele.AppendChild(port);

                port.InnerText = Userprefrenece.txt2 + "-" + Userprefrenece.txt1 + "-" + Userprefrenece.txt3;

            }

            xmldoc.Save(sFilename);

            MessageBox.Show("Saved Successfully");
        }

 

      private static void readxml(String xmlpath)
      {

            string sFilename = xmlpath;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(sFilename);

            XmlElement rootElem = xmlDoc.DocumentElement; //Gets the root element, in your xml its "Root"


            for (int i = 0; i < rootElem.ChildNodes.Count ; i++)
            {

                string name = rootElem.ChildNodes[i].Name;
                string value = rootElem.ChildNodes[i].InnerText;
                string[] words = value.Split('-');

                    txt1 = words[0];

                    txt2 = words[1];

                    txt3 = words[2];

               

                   gb.Controls["Combobox" + (i + 0).ToString()].Text = txt1;

                   gb.Controls["Textbox" + (i + 0).ToString()].Text = txt2;

                   gb.Controls["RB" + (i + 0).ToString()].Text = txt3;

                   RadioButton cb = gb.Controls["RB" + i.ToString()] as RadioButton;

                   cb.Checked = (txt3 == "True");

                   gb.Controls["RB" + (i + 0).ToString()].Text = "Enabled";

 

    }

        }

        private void button2_Click(object sender, EventArgs e)
        {

            createxml(ModifiedPath + "\\UserPreferences.xml");

        }

        private void button3_Click(object sender, EventArgs e)
        {

            readxml(ModifiedPath + "\\UserPreferences.xml");
       
        }


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend