Reputation: 137
I want to dynamically create XML file with nodes of the all TextBoxes in the form with theirs values.
Example:
var xmlNode =
new XElement("TextBoxes",
new XElement("TextBox",
new XElement("name", textBox1.Name.ToString())
)
I've tried:
foreach (TextBox text in this.Controls.OfType<TextBox>())
{
var xmlNode =
new XElement("TextBoxes",
new XElement("TextBox",
new XElement("name", text.Name.ToString())
)
);
xmlNode.Save("Test.xml");
}
Any suggestions?
Upvotes: 0
Views: 1341
Reputation: 263
You're trying to create a file for each textbox instead of one file with all the textbox values. So start by declaring your xmlNode outside of the loop and saving it afterwards :
var xmlNode = new XElement("TextBoxes");
foreach (TextBox text in this.Controls.OfType<TextBox>())
{
xmlNode.Add(new XElement("TextBox",
new XElement("name", text.Name.ToString())
)
);
}
xmlNode.Save("Test.xml");
Upvotes: 1