Jump to content

Need help using XDocument and XElement (LINQ to XML)

- - - - -

  • Please log in to reply
No replies to this topic

#1
Bodmin

Bodmin

    Newbie

  • Members
  • Pip
  • 1 posts
I am trying to learn linq and design a configuration management concept at the same time. I am looking to use linq to allow me to record the state of checkboxes and their labels on a form to an xml document. That much is not where I am having the problem. The desire is to be able to add/drop checkboxes to the form without having
then go back into the code and add or change lines to accommodate the added or dropped controls.

Doing some searching, I managed to find a way to use linq to programmatically check the form for checkboxes and
to be able to report to me the label text and the state.

The problem comes in with my grasp of XDocument and XElement. Here is a code snippet:

 private void btnSave_Click(object sender, EventArgs e)

        {

            ConfDoc = new XDocument(new XDeclaration("1.0","utf-8","yes"),new XElement("Settings"));

            

            var checks = (from Control c in this.Controls

                          select c).OfType<CheckBox>();


            foreach (var check in checks)

                if (check.Checked == true)

                {

                    textBox1.AppendText(check.Text.ToString() + "\r\n");

                    new XElement(check.Name, check.Text.ToString());

                   

                }

                    


            ConfDoc.Save(Application.StartupPath +

            @"\Test.XML", SaveOptions.None);

        }

As you can see, the use of the foreach changes the textbook explanation of the use of XDocument. Is there another approach to using this incredibly cool XML tool? :crying:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users