Click here to Skip to main content
15,921,463 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.
C#
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);
            xmlDoc.Load(sFilename);
            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;
            xmlDoc.DocumentElement.AppendChild(elmXML);
            xmlDoc.Save(sFilename);
            MessageBox.Show("Saved Successfully");

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

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

Regards,
Lakshmi Narayanan.S
Posted
Updated 24-May-11 18:57pm
v2

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
 
Comments
Sergey Alexandrovich Kryukov 26-May-11 12:45pm    
Good approach, my 5.
I added my solution based on Data Contract, please see.
--SA
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.

See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx[^].

—SA
 
Share this answer
 
Comments
Sandeep Mewara 26-May-11 12:47pm    
Nice alternative... 5!
Sergey Alexandrovich Kryukov 26-May-11 12:49pm    
Thank you, Sandeep.
--SA
Espen Harlinn 26-May-11 13:29pm    
Another obviously good solution, my 5
Sergey Alexandrovich Kryukov 26-May-11 13:32pm    
Thank you, Espen.
--SA
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.
Regards,
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
 
Comments
naraayanan 25-May-11 1:14am    
Hi Meware,
thanks for your reply. I tried it. It's not working .I go to 3rd link.
Regards,
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?
--SA
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.
--SA
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