0

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?

3
  • Why is your solution not working ? Do you get an exception ? Commented May 21, 2015 at 9:38
  • It is working but it's writing in XML only one TexBox value, just the first texbox. Commented May 21, 2015 at 9:43
  • 1
    try moving the Save out of the loop Commented May 21, 2015 at 9:45

1 Answer 1

1

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"); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.