Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I explain my Project
Step:1
I created a Dynamic textbox.(eg:4)
Step:2
I saved data into xmlfile based on textbox.
Step:3
I retrieve from xmlfile based on textbox(on that save format).
I finished first 2 steps. my problem is 3 step.
Please Help me.
I send my code for u reference:

Declaration part
public static System.Windows.Forms.GroupBox gb;
public static string ModifiedPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\Folder\\dataText";
Step:1
To create a groupbox and textbox:
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 = 300;
gb.Height = 150;
gb.Left = 10;
gb.Top = 15;
this.Controls.Add(gb);
int i;
for (i = 0; i < 4; i++)
   {
   TextBox txt = new TextBox();
   txt.Name = "Textbox" + i.ToString();
   txt.Text = "Testtext" + i.ToString();
   txt.Width = 150;
   txt.Height = 20;
   txt.Left = 100;
   txt.Top = 20 + (txt.Height * i) + (10 * i);

   gb.Controls.Add(txt);
   }


Step:2
To create a xmlfile and saved data from textbox.
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 (int index = 0; index < 4; index++)
   {
   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 str = "port" + index.ToString();
   XmlElement port = xmldoc.CreateElement(str);
   xmlele.AppendChild(port);
   port.InnerText = Userprefrenece.txt1;
   }
xmldoc.Save(sFilename);
MessageBox.Show("Saved Successfully");

In my XML file data:
<?xml version="1.0"?>
<Root>
<port0>NARAYANAN</port0>
<port1>DELL</port1>
<port2>C#.NET</port2>
<port3>SUCCESS</port3>
</Root>

Step:3
??????(What will i do)
My output like this,
textbox1 =Port0 data
textbox2 =Port1 data
textbox3 =Port3 data
textbox4 =Port4 data


Regards,
Lakshmi Narayanan.S


[edit]Code blocks added, some formatting - OriginalGriff[/edit]
Posted
Updated 24-May-11 20:52pm
v2
Comments
Ankit Rajput 25-May-11 3:06am    
What is the problem?
Sergey Alexandrovich Kryukov 25-May-11 3:07am    
You never explain what you want to do. As to Step 3, it looks like you want to read your mind.
--SA
naraayanan 25-May-11 4:43am    
i want <port0> data in textbox1,<port1> data in textbox2 and so on...
Text boxes create dynamically,Please help me
Kim Togo 25-May-11 3:18am    
Do you want to read the XML file and display <port0> value at textbox1 etc. ?
naraayanan 25-May-11 4:41am    
yes ,But Text boxes create Dynamically.Please help me

I am not able to understand completely what you want in the third step.
If you want to read the xml and put it in those 4 textboxes , then here is how it goes:
C#
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].Value;
    gb.Controls["Textbox" + (i + 1).ToString()].Text = name + " " + value;
}


Try now.
 
Share this answer
 
v3
Comments
naraayanan 25-May-11 4:27am    
Hi Tarun,
Thanks for u reply.I created a dynamic textbox.it's explain in Step 1.But my problem How to store a XML Data to Dynamic textbox(as proper way)
Regards,
Lakshmi Narayanan.S
Tarun.K.S 25-May-11 5:00am    
I have updated the answer. Check now.
naraayanan 25-May-11 5:09am    
Thanks Tarun.and thanks to every one.but some change in your code .Please see this
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 ;
gb.Controls["Textbox" + (i + 0).ToString()].Text = value; ;

}
Regards,
Lakshmi Narayanan.S
Tarun.K.S 25-May-11 5:15am    
Ya right, Innertext should be used, thanks for the pointer. Do vote and accept the answer if it helped.
naraayanan 25-May-11 7:31am    
Hi,
Thanks for help. I need another help.I have a Checkbox/Radio button in my form .I created Dynamically.
I have a 2 check box (created Dynamically).
One box is checked.Another one is unchecked.i saved data into XML.How can i get the value.Please clear my doubt.
Hi Lakshmi,
I saw your issue, look my code below, it may be helpful.
string name, text, targetTextbox;
XmlDocument xml = new XmlDocument();
xml.Load(sFilename);
foreach (XmlElement item in xml.DocumentElement.ChildNodes)
{
    name = item.Name;
    text = item.InnerText;
    targetTextbox = name.Remove(0, 4);
    string targetTextBox = "Textbox" + index;
    //Try to find the textbox
    int textBoxIndex = gb.Controls.IndexOfKey(targetTextBox);
    if (textBoxIndex != -1)
    {
        TextBox foundTextBox = (TextBox)gb.Controls[textBoxIndex];
        foundTextBox.Text = text;
    }
}


Xiaobei
 
Share this answer
 
Comments
Tarun.K.S 25-May-11 3:45am    
What if Lakshmi renamed the textboxes? :rolleyes:
naraayanan 25-May-11 4:13am    
Hi Xiaobei,
Thanks for help.I try it but it's not working . i send my code for u reference
string sFilename = xmlpath;
string name, text, targetTextbox;
XmlDocument xml = new XmlDocument();
xml.Load(sFilename);
for (int index = 0; index < 4; index++)
{
foreach (XmlElement item in xml.DocumentElement.ChildNodes)
{
name = item.Name;
text = item.InnerText;
targetTextbox = name.Remove(0, 4);
string targetTextBox = "Textbox" + index;
//Try to find the textbox
int textBoxIndex = gb.Controls.IndexOfKey(targetTextBox);
if (textBoxIndex != -1)
{
TextBox foundTextBox = (TextBox)gb.Controls[textBoxIndex];
foundTextBox.Text = text;
}
}
}
Regards,
Lakshmi Narayanan.S
Please help me
Xiaobei Zhang 25-May-11 21:40pm    
Lakshmi,
Sorry for late.
Added "FOR" loop is not needed because I have used "Foreach" loop to loop through each node in the XML file.
I'm sorry for my mistake: I copy some code from you but i forget to change the name of the variable...
Look my code below:


string name, text, targetTextboxIndex;
XmlDocument xml = new XmlDocument();
xml.Load(sFilename);
foreach (XmlElement item in xml.DocumentElement.ChildNodes)
{
name = item.Name;
text = item.InnerText;
//Tarun's method is also good,simple and useful.
//But I think You can config a name mapping between XML node name and Textbox name to replace the HARDCODE.
targetTextboxIndex = name.Remove(0, 4);
string targetTextBox = "Textbox" + targetTextboxIndex;
gb.Controls[targetTextBox].Text = text;
}

Xiaobei
Xiaobei Zhang 25-May-11 21:24pm    
Hi Tarun,
I think Lakshmi can create a config file to mapping XML node name and Textbox name to solve this issue.
Tarun.K.S 26-May-11 2:55am    
Hmmm right, her XML is not well structured.

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