在.NET操作XML的最佳方式 [英] Best way to manipulate XML in .NET

查看:134
本文介绍了在.NET操作XML的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理现有的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?

有很多.NET类的XML处理的,我不知道会是这样做的最佳方法。

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

推荐答案

如果它确实是一个巨大的XML无法装入内存,你应该使用的的XmlReader /的的XmlWriter 。如果没有 LINQ到XML 是很容易使用。如果没有.NET 3.5,你可以使用的XmlDocument

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");

这篇关于在.NET操作XML的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆