Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all Artist node from an XML file

Tags:

c#

.net

xml

I have an XmlWriter that contains a xml that looks like the one below, just with a lot more nodes. what's the fastest and best way to remove all the ARTIST node from this xml ?

<CATALOG> 
  <CD> 
    <TITLE>Empire Burlesque</TITLE> 
    <ARTIST>Bob Dylan</ARTIST> 
  </CD> 
  <CD> 
    <TITLE>Hide your heart</TITLE> 
    <ARTIST>Bonnie Tyler</ARTIST>
  </CD>
</CATALOG> 
like image 880
Erez Avatar asked Dec 21 '25 07:12

Erez


1 Answers

As long as the file isn't gigabytes XmlDocument should be fine:

    XmlDocument XDoc = new XmlDocument();
    XDoc.Load(MapPath(@"~\xml\test.xml"));

    XmlNodeList Nodes = XDoc.GetElementsByTagName("ARTIST");

    foreach (XmlNode Node in Nodes)
        Node.ParentNode.RemoveChild(Node);

    XDoc.Save(MapPath(@"~\xml\test_out.xml"));
like image 167
Steve Wellens Avatar answered Dec 23 '25 21:12

Steve Wellens