Search This Blog

Tuesday, May 20, 2014

XElement C# Example

C# > LINQ > LINQ TO XML > XElement

XElement is an XML element.
With XElement you can create, change, delete elements.

Example


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;






namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XElement coTree = new XElement("Company",
                new XElement("Dept", 1),
                new XElement("Dept", 2),
                new XElement("Dept", 3));

            XElement co1Tree = new XElement("Company1",
                new XElement("CEO", 1),
                new XElement("Manager", 2),
                from el in coTree.Elements()
                    where (int)el < 3 
                        select el);
            textBox1.Text = co1Tree.ToString();
 
        }
    }
}