---
title: "Saving Values In Xml And Retrieving Them"  
description: "Saving Values In Xml And Retrieving Them"  
author: "Jayden Bell"  
published: 2014-10-21  
updated: 2014-10-21  
canonical: https://www.mindstick.com/forum/2429/saving-values-in-xml-and-retrieving-them  
category: "xml"  
tags: ["xml", "xaml"]  
reading_time: 2 minutes  

---

# Saving Values In Xml And Retrieving Them

i have a 4 to 5 textboxes and 2buttons to save and [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) the values back, after clicking on save [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) all the [textbox values](https://www.mindstick.com/forum/34164/how-to-print-textbox-values-and-datagridview-using-print-control-in-windows-forms-in-vb-dot-net) should be saved in [xml file](https://www.mindstick.com/forum/360/how-to-read-xml-file-in-php),and when i [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) on retrieve button the values from xml file should be retrieved in [listbox](https://www.mindstick.com/articles/626/listbox-events-in-c-dot-net) or listview.

## Replies

### Reply by marcel ethan

Try this:\
\
using System.[Xml](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server);using System.Xml.Linq;\
\
// ....\
\
public partial class Form1 : Form { public Form1() { InitializeComponent(); btnRetrieve.Enabled = false; }\
private string filePath = @"c:\myfiles\whatever.xml";\
private void btnSave_Click(object sender, EventArgs e) { XElement textBoxes = new XElement("textboxes");\
// assumes all textboxes placed directly on form int count = this.Controls.Count; for(int i = count - 1; i >= 0; i--) { Control c = this.Controls[i];\
if (c is [TextBox](https://www.mindstick.com/forum/12714/dynamic-textbox-in-gridview)) { XElement textBox = new XElement("textbox"); textBox.Add(new XAttribute("name", c.Name)); textBox.Value = c.Text; textBoxes.Add(textBox); } }\
textBoxes.Save (filePath); btnRetrieve.Enabled = true; }\
private void btnRetrieve_Click(object sender, EventArgs e) { XElement textBoxes = XElement.Load(filePath); foreach (XElement textBox in textBoxes.Elements("textbox")) { listBox1.Items.Add(textBox.Value); } listBox1.SelectedIndex = 0; } }\
\
\
If you're wondering why I'm iterating backwards through the controls collection to retrieve the textbox [values](https://www.mindstick.com/forum/327/sum-textbox-values), that's because (for some unknown reason) the VS designer adds them to the collection in the reverse order they were added to the form.\


---

Original Source: https://www.mindstick.com/forum/2429/saving-values-in-xml-and-retrieving-them

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
