Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML
Tip/Trick

Delete an XML node using the node name

Rate me:
Please Sign up or sign in to vote.
4.78/5 (2 votes)
23 Aug 2011CPOL 44K   2   2
How to delete an XML node using its name.

Using xml.linq we can delete a node from XML in C#.


C#
XDocument XMLDoc = XDocument.Load(path);
XElement elment = (from xml1 in XMLDoc.Descendants("Node")
                   select xml1).FirstOrDefault();
elment.Remove();
XMLDoc.Save(path);

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionQuicker approach Pin
NitinNayak29-Jul-15 20:39
NitinNayak29-Jul-15 20:39 
GeneralReason for my vote of 3 Good, but it will fail if no Element... Pin
Kim Togo30-Aug-11 1:51
professionalKim Togo30-Aug-11 1:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.