Remove an element in CSharp

Description

The following code shows how to remove an element.

Example


/*from  w  w w.j a v a  2s  .c  o  m*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Linq;

class Program
{
    static void Main(string[] args)
    {
        string doc = @"<people>
                          <person>
                            <id>1</id>
                            <firstname>C</firstname>
                            <lastname>L</lastname>
                            <idrole>1</idrole>
                            </person>
                       </people>";
        XElement xml = XElement.Parse(doc);
        xml.Descendants("person").First().Remove();
        Console.WriteLine(xml);
    }
}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style