Click here to Skip to main content
15,936,119 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,

I created a Dynamic textbox and insert a value into XML.I send my code for u reference.
My code for XML Creation.
string sFilename = xmlpath;
            XmlDocument xmlDoc = new XmlDocument();
            String XmlSpecification = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
            XmlSpecification = XmlSpecification + Environment.NewLine + "<Testing></Testing>";
            File.AppendAllText(xmlpath, XmlSpecification);
            XmlElement elmXML = xmlDoc.CreateElement("Configuration");
            string strConfiguration = String.Empty;
            for (int index = 0; index < 3; 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();
                strConfiguration = strConfiguration + Environment.NewLine + @"<str>"  + Userprefrenece.txt1 + "</str>";
            elmXML.InnerXml = strConfiguration;
            MessageBox.Show("Saved Successfully");

I saved successfully. How can retrieve from XML. But I didn't get correct.
My XML Data
<?xml version="1.0" encoding="utf-8" ?>
- <Testing>
- <Configuration>

retrieve is my problem. How can I retrieve data from xml.and store in the textbox .Please help me Urgent

Lakshmi Narayanan.S
Updated 24-May-11 18:57pm

Apart from the options S Mewara gave you, you can try out xsd.exe.

Create a schema for your xml and run the XML Schema Definition Tool[^] on the xsd file.

You will then have typesafe classes the allows you to manipulate the contents of the xml file.

Best reagrds
Espen Harlinn
Share this answer
Sergey Alexandrovich Kryukov 26-May-11 12:45pm    
Good approach, my 5.
I added my solution based on Data Contract, please see.
Espen Harlinn 26-May-11 13:29pm    
Thank you, SAKryukov!
Sandeep Mewara 26-May-11 12:47pm    
Good one. My 5 too!
Espen Harlinn 26-May-11 13:29pm    
Thank you, S Mewara!
Sandeep Mewara 26-May-11 14:30pm    
You know my name Espen, you can call me just Sandeep. It will do. :)
I would suggest you use different approach. If you need to persist some data, not matter what kind, you don't have to work with XML directly.

You can use Data Contract. This is the most non-intrusive way to add persistence to and arbitrary data type structure. You just add attributes [DataContract] and [DataMember]. When you have some data graph formed by the instances of you data classes participated in the contract (this graph does not even have to be a tree: circular references will be successfully resolved automatically), you can use System.Runtime.Serialization.DataContractSerializer to save and restore data to any stream.

Your XML schema will be created on the fly. With Data Contract, the part of contract is only what is marked with the attributes. You can change anything else and be not afraid of the risk of breaking the contract. It's also very easy to evolve your data schema incrementally as you develop new versions of your software: you just add new contract members/types. You won't need to create any kind of conversion from the data of old version to a new version.


Share this answer
Sandeep Mewara 26-May-11 12:47pm    
Nice alternative... 5!
Sergey Alexandrovich Kryukov 26-May-11 12:49pm    
Thank you, Sandeep.
Espen Harlinn 26-May-11 13:29pm    
Another obviously good solution, my 5
Sergey Alexandrovich Kryukov 26-May-11 13:32pm    
Thank you, Espen.
naraayanan 27-May-11 7:39am    
Hi friends ,
thanks for u reply.Could u send any project for this DataContractSerializer.This is really help to improve my knowledge.
Lakshmi Narayanan.S
The System.Xml namespace provides functionality for reading, writing and navigating XML data.

Following links should help:
How To Read XML Data into a DataSet by Using Visual C# .NET[^]
Retrieve random data from xml file[^]
Reading data from an XML file using C#[^]
Share this answer
naraayanan 25-May-11 1:14am    
Hi Meware,
thanks for your reply. I tried it. It's not working .I go to 3rd link.
Lakshmi Narayanan.S
Please give any other link.Thanks in advance
Sergey Alexandrovich Kryukov 26-May-11 12:33pm    
Giving "other links" cannot make the program working. What's the problem?
Sergey Alexandrovich Kryukov 26-May-11 12:46pm    
I don't see why do you have to waste your time on this -- please see my solution.
Espen Harlinn 25-May-11 16:44pm    
Nice set of links, my 5
Sandeep Mewara 26-May-11 14:31pm    
Thanks to you too.

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