How to delete nodes and subnodes using XDocument

Niladri.Biswas
Posted by in C# category on for Beginner level | Points: 250 | Views : 4348 red flag

In this article we will look as how to delete nodes and subnodes using XDocument.

Introduction

In this article we will look as how to delete nodes and subnodes using XDocument.

Straight to Experiment

Say we have an xml as under

<databaseserver>
   <server name="server1" serverid="1">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
   <server name="server2" serverid="2">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
   <server name="server3" serverid="3">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
</databaseserver>

We want to delete the nodes and it's subnodes whose serverid="2".Final output will be

<databaseserver>
   <server name="server1" serverid="1">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>   
   <server name="server3" serverid="3">
    <databases>
            <database dbid="1" dbname="db1" />
        <database dbid="2" dbname="db2" />
    </databases>
   </server>
</databaseserver>

Let us write the below program to do so

using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = @"<databaseserver>
                               <server name='server1' serverid='1'>
                                <databases>
                                        <database dbid='1' dbname='db1' />
                                    <database dbid='2' dbname='db2' />
                                </databases>
                               </server>
                               <server name='server2' serverid='2'>
                                <databases>
                                        <database dbid='1' dbname='db1' />
                                    <database dbid='2' dbname='db2' />
                                </databases>
                               </server>
                               <server name='server3' serverid='3'>
                                <databases>
                                        <database dbid='1' dbname='db1' />
                                    <database dbid='2' dbname='db2' />
                                </databases>
                               </server>
                        </databaseserver>";

            var xDoc = XDocument.Parse(input);

            xDoc.Descendants("server")
                .First(s => s.Attribute("serverid").Value == "2")
                .Remove();

            var newXml = xDoc.ToString();
        }
    }
}

Here, first we are creating a new System.Xml.Linq.XDocument from a string by using the "Parse" method.Then we are using the "Descendants" function that returns a filtered collection of the descendant elements for this document or element, in document order. Only elements that have a matching System.Xml.Linq.XName are included in the collection.We are using the "First" function that returns the first element in a sequence that satisfies a specified condition.Finally, the "Remove" method removes the node from its parent.And then we are displaying the new xml

Conclusion

In this article we looked as how to delete nodes and subnodes using XDocument.Hope this will be helpful.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)