Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have some items in a listbox, and when i click on the particular item, i need to enter the corresponding value in a text box, and these have to be saved in an xml. For example. my listbox contains name and age. I click on name, enter the corresponding value(say John) in the text box and i need to save this to an xml. This has to be done for all the items in the listbox. I am a beginner and not too sure how to go about it. I hope my question is comprehensbile. How can i go about this? Thanks!
Posted

There are few things you need to decide first:

1- Structure of xml file that you want to write, it may be something like this:

HTML
<people>
<name>John</name>
<age>26</age>
</people>
<people>
<name>xyz</name>
<age>28</age>
.
.
</people>


Or you can come up with something else

2- When the data needs to be written in xml file, immediately after one set of data is captured or only at the end when all data sets are captured.

After this you can use following link to write data into xml file:

http://forum.codecall.net/csharp-tutorials/31315-c-tutorial-reading-writing-xml-files.html

Hope it helps...
 
Share this answer
 
Comments
bizzare1988 13-Sep-11 2:24am    
Hi,To answer your question:

1) The structure i was looking for the xml file would be something like this:

Collapse | Copy Code<people><person name="John" age="30">

<person name="anna" age="22">..

</person></person></people>


2) Thats a good suggestion. The data should be written after all the set of data are captured. (But this wont make me loose the other values right?)

Thanks a lot, that link really helped! But im still not sure on how to add it from the listbox and the value from the textbox :(
i have this now:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("Query_advanced.xml", settings);
            writer.WriteStartDocument();
            writer.WriteStartElement("Query");
            foreach (string item in listBox1.SelectedItems)
            {
                               
                    writer.WriteStartElement("Attribute");
                    writer.WriteAttributeString("Name", item);
                    //writer.WriteAttributeString("Name", "Patient Name");
                    writer.WriteAttributeString("Value", textBox1.Text);
                    writer.WriteEndElement();
                
                //   catch
                //{
                //    throw;
                //}
                
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
            
        }


And i need to save the items before coming out of the loop, and then update the xml everytime for this. How can i go about?
 
Share this answer
 

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