Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to manipulate XML in .NET

Tags:

c#

.net

xml

I need to manipulate an existing XML document, and create a new one from it, removing a few nodes and attributes, and perhaps adding new ones, what would be the best group of classes to accomplish this?

There are a lot of .NET classes for XML manipulation, and I'm not sure what would be the optimal way to do it.

like image 586
juan Avatar asked Mar 18 '10 13:03

juan


1 Answers

If it is a really huge XML which cannot fit into memory you should use XmlReader/XmlWriter. If not LINQ to XML is very easy to use. If you don't have .NET 3.5 you could use XmlDocument.

Here's an example of removing a node:

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

var doc = XElement.Load("test.xml");
doc.XPathSelectElement("//customer").Remove();
doc.Save("test.xml");
like image 160
Darin Dimitrov Avatar answered Oct 09 '22 10:10

Darin Dimitrov